@@ -6,10 +6,97 @@ order: 10
6
6
7
7
# Chapter 10 Outlook: Introduction of C++20
8
8
9
+ [ TOC]
9
10
11
+ C++20 seems to be an exciting update.
12
+ For example, as early as C++11, the ` Concept ` ,
13
+ which was eager to call for high-altitude but ultimately lost, is now on the line.
14
+ The C++ Organizing Committee decided to vote to finalize C++20 with many proposals,
15
+ such as ** Concepts** /** Module** /** Coroutine** /** Ranges** / and so on.
16
+ In this chapter we'll take a look at some of the important features that
17
+ C++20 will introduce.
18
+
19
+ ## Concept
20
+
21
+ Concept is a further enhancement to C++ template programming.
22
+ In simple terms, the concept is a compile-time feature.
23
+ It allows the compiler to evaluate template parameters at compile time,
24
+ greatly enhancing our experience with template programming in C++.
25
+ When programming with templates, we often encounter a variety of heinous errors.
26
+ This is because we have so far been unable to check and limit template parameters.
27
+ For example, the following two lines of code can cause a lot of
28
+ almost unreadable compilation errors:
29
+
30
+ ``` cpp
31
+ #include < list>
32
+ #include < algorithm>
33
+ int main () {
34
+ std::list<int> l = {1, 2, 3};
35
+ std::sort (l.begin(), l.end());
36
+ return 0;
37
+ }
38
+ ```
39
+
40
+ The root cause of this code error is that ` std::sort ` must provide
41
+ a random iterator for the sorting container, otherwise it will not be used,
42
+ and we know that ` std::list ` does not support random access.
43
+ In the conceptual language, the iterator in ` std::list ` does not satisfy
44
+ the constraint of the concept of random iterators in ` std::sort ` .
45
+ After introducing the concept, we can constrain the template parameters
46
+ like this:
47
+
48
+ ``` cpp
49
+ template <typename T>
50
+ requires Sortable<T> // Sortable is a concept
51
+ void sort (T& c);
52
+ ```
53
+
54
+ abbreviate as:
55
+
56
+ ```cpp
57
+ template<Sortable T> // T is a Sortable typename
58
+ void sort(T& c)
59
+ ```
60
+
61
+ Even use it directly as a type:
62
+
63
+ ``` cpp
64
+ void sort (Sortable& c); // c is a Sortable type object
65
+ ```
66
+
67
+ Let's look at a practical example.
68
+
69
+ TODO
70
+
71
+ ## Module
72
+
73
+ TODO
74
+
75
+ ## Contract
76
+
77
+ TODO
78
+
79
+ ## Range
80
+
81
+ TODO
82
+
83
+ ## Coroutine
84
+
85
+ TODO
86
+
87
+ ## Conclusion
88
+
89
+ In general, I finally saw the exciting features of Concepts/Ranges/Modules in C++20.
90
+ This is still full of charm for a programming language that is already in its thirties.
10
91
11
92
[Table of Content](./toc.md) | [Previous Chapter](./09-others.md) | [Next Chapter](./appendix1.md)
12
93
94
+ ## Further Readings
95
+
96
+ - [Why Concepts didn't make C++17?](http://honermann.net/blog/2016/03/06/why-concepts-didnt-make-cxx17/)
97
+ - [C++11/14/17/20 Compiler Support](http://en.cppreference.com/w/cpp/compiler_support)
98
+ - [C++ History](https://en.cppreference.com/w/cpp/language/history)
99
+
13
100
## Licenses
14
101
15
102
<a rel="license" href="http://creativecommons.org/licenses/by-nc-nd/4.0/"><img alt="Creative Commons License" style="border-width:0" src="https://i.creativecommons.org/l/by-nc-nd/4.0/88x31.png" /></a><br />This work was written by [Ou Changkun](https://changkun.de) and licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc-nd/4.0/">Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License</a>. The code of this repository is open sourced under the [MIT license](../../LICENSE).
0 commit comments