Skip to content

Commit 3fc59f9

Browse files
committed
see #12: translate ch10
1 parent c50f265 commit 3fc59f9

File tree

2 files changed

+89
-4
lines changed

2 files changed

+89
-4
lines changed

book/en-us/10-cpp20.md

+87
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,97 @@ order: 10
66

77
# Chapter 10 Outlook: Introduction of C++20
88

9+
[TOC]
910

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.
1091
1192
[Table of Content](./toc.md) | [Previous Chapter](./09-others.md) | [Next Chapter](./appendix1.md)
1293
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+
13100
## Licenses
14101
15102
<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).

book/zh-cn/10-cpp20.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ order: 10
88

99
[TOC]
1010

11-
C++20 如同 C++17 一样,似乎能够成为一个振奋人心的更新。例如,早在 C++11 时期就跃跃欲试呼声极高却最终落选的 `Concept`,如今已经箭在弦上。
11+
C++20 如同 C++11 一样,似乎能够成为一个振奋人心的更新。例如,早在 C++11 时期就跃跃欲试呼声极高却最终落选的 `Concept`,如今已经箭在弦上。
1212
C++ 组委会在讨论投票最终确定 C++20 有很多提案,诸如 **Concepts**/**Module**/**Coroutine**/**Ranges**/ 等等。
1313
本章我们就来一览 C++20 即将引入的那些重要特性。
1414

@@ -75,16 +75,14 @@ TODO
7575
7676
## 总结
7777
78-
总的来说,终于在 C++2a 中看到 Concepts/Ranges/Modules 这些令人兴奋的特性,
78+
总的来说,终于在 C++20 中看到 Concepts/Ranges/Modules 这些令人兴奋的特性,
7979
这对于一门已经三十多岁『高龄』的编程语言,依然是充满魅力的。
8080
8181
[返回目录](./toc.md) | [上一章](./09-others.md) | [下一章 进一步阅读的学习材料](./appendix1.md)
8282
8383
8484
## 进一步阅读的参考资料
8585
86-
- [Final features of C++17](https://meetingcpp.com/index.php/br/items/final-features-of-c17.html)
87-
- [C++17: will it be great or just ok?](https://codeplay.com/public/uploaded/filehost/0cbdaf_c++17post-oulu2016.pdf)
8886
- [Why Concepts didn't make C++17?](http://honermann.net/blog/2016/03/06/why-concepts-didnt-make-cxx17/)
8987
- [C++11/14/17/20 编译器支持情况](http://en.cppreference.com/w/cpp/compiler_support)
9088
- [C++ 历史](https://en.cppreference.com/w/cpp/language/history)

0 commit comments

Comments
 (0)