Skip to content

Commit

Permalink
see changkun#12: translate ch10
Browse files Browse the repository at this point in the history
  • Loading branch information
changkun authored and HarrisonDing committed Feb 8, 2020
1 parent cbd01a9 commit b08c14b
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 4 deletions.
87 changes: 87 additions & 0 deletions book/en-us/10-cpp20.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,97 @@ order: 10

# Chapter 10 Outlook: Introduction of C++20

[TOC]

C++20 seems to be an exciting update.
For example, as early as C++11, the `Concept`,
which was eager to call for high-altitude but ultimately lost, is now on the line.
The C++ Organizing Committee decided to vote to finalize C++20 with many proposals,
such as **Concepts**/**Module**/**Coroutine**/**Ranges**/ and so on.
In this chapter we'll take a look at some of the important features that
C++20 will introduce.

## Concept

Concept is a further enhancement to C++ template programming.
In simple terms, the concept is a compile-time feature.
It allows the compiler to evaluate template parameters at compile time,
greatly enhancing our experience with template programming in C++.
When programming with templates, we often encounter a variety of heinous errors.
This is because we have so far been unable to check and limit template parameters.
For example, the following two lines of code can cause a lot of
almost unreadable compilation errors:

```cpp
#include <list>
#include <algorithm>
int main() {
std::list<int> l = {1, 2, 3};
std::sort(l.begin(), l.end());
return 0;
}
```

The root cause of this code error is that `std::sort` must provide
a random iterator for the sorting container, otherwise it will not be used,
and we know that `std::list` does not support random access.
In the conceptual language, the iterator in `std::list` does not satisfy
the constraint of the concept of random iterators in `std::sort`.
After introducing the concept, we can constrain the template parameters
like this:

```cpp
template <typename T>
requires Sortable<T> // Sortable is a concept
void sort(T& c);
```
abbreviate as:
```cpp
template<Sortable T> // T is a Sortable typename
void sort(T& c)
```

Even use it directly as a type:

```cpp
void sort(Sortable& c); // c is a Sortable type object
```
Let's look at a practical example.
TODO
## Module
TODO
## Contract
TODO
## Range
TODO
## Coroutine
TODO
## Conclusion
In general, I finally saw the exciting features of Concepts/Ranges/Modules in C++20.
This is still full of charm for a programming language that is already in its thirties.
[Table of Content](./toc.md) | [Previous Chapter](./09-others.md) | [Next Chapter](./appendix1.md)
## Further Readings
- [Why Concepts didn't make C++17?](http://honermann.net/blog/2016/03/06/why-concepts-didnt-make-cxx17/)
- [C++11/14/17/20 Compiler Support](http://en.cppreference.com/w/cpp/compiler_support)
- [C++ History](https://en.cppreference.com/w/cpp/language/history)
## Licenses
<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).
6 changes: 2 additions & 4 deletions book/zh-cn/10-cpp20.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ order: 10

[TOC]

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

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

0 comments on commit b08c14b

Please sign in to comment.