diff --git a/assets/donate.md b/assets/donate.md index 3911899..88ca78d 100644 --- a/assets/donate.md +++ b/assets/donate.md @@ -8,7 +8,7 @@ order: 1 I would love if you support me to make the book better: -[![](https://img.shields.io/badge/donate-PayPal-104098.svg?style=popout-square&logo=PayPal)](https://www.paypal.me/ouchangkun/4.99eur) +[![](https://img.shields.io/badge/donate-PayPal-104098.svg?style=popout-square&logo=PayPal)](https://www.paypal.me/changkunde/4.99eur) ## 资助 diff --git a/book/zh-cn/02-usability.md b/book/zh-cn/02-usability.md index 8c42600..fd28d0f 100644 --- a/book/zh-cn/02-usability.md +++ b/book/zh-cn/02-usability.md @@ -651,7 +651,7 @@ class Magic>> darkMagic; ``` -既然是任意形式,所以个数为0的模板参数也是可以的:`class Magic<> nothing;`。 +既然是任意形式,所以个数为 0 的模板参数也是可以的:`class Magic<> nothing;`。 如果不希望产生的模板参数个数为0,可以手动的定义至少一个模板参数: @@ -670,8 +670,8 @@ template void printf(const std::string &str, Args... args); 首先,我们可以使用 `sizeof...` 来计算参数的个数,: ```cpp -template -void magic(Args... args) { +template +void magic(Ts... args) { std::cout << sizeof...(args) << std::endl; } ``` @@ -692,20 +692,22 @@ magic(1, ""); // 输出2 ```cpp #include template -void printf(T0 value) { +void printf1(T0 value) { std::cout << value << std::endl; } -template -void printf(T value, Args... args) { +template +void printf1(T value, Ts... args) { std::cout << value << std::endl; - printf(args...); + printf1(args...); } int main() { -printf(1, 2, "123", 1.1); -return 0; + printf1(1, 2, "123", 1.1); + return 0; } ``` +**2. 变参模板展开** + 你应该感受到了这很繁琐,在 C++17 中增加了变参模板展开的支持,于是你可以在一个函数中完成 `printf` 的编写: ```cpp @@ -716,39 +718,8 @@ void printf(T0 t0, T... t) { } ``` - - -**2. 初始化列表展开** - -> 这个方法需要之后介绍的知识,读者可以简单阅读一下,将这个代码段保存,在后面的内容了解过了之后再回过头来阅读此处方法会大有收获。 - -递归模板函数是一种标准的做法,但缺点显而易见的在于必须定义一个终止递归的函数。 - -这里介绍一种使用初始化列表展开的黑魔法: - -```cpp -// 编译这个代码需要开启 -std=c++14 -template -auto print(T value, Args... args) { - std::cout << value << std::endl; - return std::initializer_list{([&] { - std::cout << args << std::endl; - }(), value)...}; -} -int main() { - print(1, 2.1, "123"); - return 0; -} -``` - -在这个代码中,额外使用了 C++11 中提供的初始化列表以及 Lambda 表达式的特性(下一节中将提到),而 std::initializer_list 也是 C++11 新引入的容器(以后会介绍到)。 - -通过初始化列表,`(lambda 表达式, value)...` 将会被展开。由于逗号表达式的出现,首先会执行前面的 lambda 表达式,完成参数的输出。唯一不美观的地方在于如果不使用 `return` 编译器会给出未使用的变量作为警告。 - > 事实上,有时候我们虽然使用了变参模板,却不一定需要对参数做逐个遍历,我们可以利用 `std::bind` 及完美转发等特性实现对函数和参数的绑定,从而达到成功调用的目的。 -> 关于这方面的使用技巧,请参考习题,TODO - ### 折叠表达式 C++ 17 中将变长参数这种特性进一步带给了表达式,考虑下面这个例子: @@ -764,6 +735,51 @@ int main() { } ``` +### 非类型模板参数推导 + +前面我们主要提及的是模板参数的一种形式:类型模板参数。 + +```cpp +template + auto add(T t, U u) { + return t+u; +} +``` + +其中模板的参数 `T` 和 `U` 为具体的类型。 +但还有一种常见模板参数形式可以让不同字面量成为模板参数,即非类型模板参数: + +```cpp +template +class buffer_t { +public: + T& alloc(); + void free(T& item); +private: + T data[BufSize]; +} + +buffer_t buf; // 100 作为模板参数 +``` + +在这种模板参数形式下,我们可以将 `100` 作为模板的参数进行传递。 +在 C++11 引入了类型推导这一特性后,我们会很自然的问,既然此处的模板参数 +以具体的字面量进行传递,能否让编译器辅助我们进行类型推导, +通过使用占位符 `auto` 从而不再需要明确指明类型? +幸运的是,C++17 引入了这一特性,我们的确可以 `auto` 关键字,让编译器辅助完成具体类型的推导, +例如: + +```cpp +template void foo() { + std::cout << value << std::endl; + return; +} + +int main() { + foo<10>(); // value 被推导为 int 类型 +} +``` + ## 2.6 面向对象 ### 委托构造 diff --git a/book/zh-cn/10-cpp20.md b/book/zh-cn/10-cpp20.md index 611c67c..c50958f 100644 --- a/book/zh-cn/10-cpp20.md +++ b/book/zh-cn/10-cpp20.md @@ -12,49 +12,9 @@ order: 10 本章对即将到来的 C++17 进行介绍,几个月前(2016 年),目前为止,还没有一个正式发布的编译器来编译 C++17 特性的代码,本节作为扩展主题,供对 C++ 的历史进程及其未来发展感兴趣的读者阅读。 -## 10.1 主要入选特性 - -### 非类型模板参数的 auto - -模板参数分为两种,一种是类型模板参数,也是我们用得最多的一种: - -```cpp -template - auto add(T t, U u) { - return t+u; -} -``` - -里面的 `T` 和 `U` 都是类型模板参数。另一种是非类型模板参数,它可以让不同的字面量成为模板的参数: - -```cpp -template -class buffer_t { -public: - T& alloc(); - void free(T& item); -private: - T data[BufSize]; -} - -buffer_t buf; // 100 作为模板参数 -``` - -遗憾的是我们在编写模板的时候就必须明确非类型模板参数的具体类型,C++17 打破了这一限制,让我们能够在非类型模板参数中使用 `auto` 关键字,从而让编译器推导具体的类型: - -```cpp -template void foo() { - return; -} - -foo<10>(); // value 被推导为 int 类型 -``` - -## 10.2 未入选特性 - C++ 组委会在讨论投票最终确定 C++17 有很多提案,诸如 **Concepts**/**Ranges**/**Module** 等等,其中最受关注的就是 **Concepts**,可惜这一提案最终被拒,作为技术规范(Technical Specifications, TS) 将其发布。 -### Concepts TS +## Concepts TS **Concepts** 是对 C++ 模板编程的进一步增强扩展。简单来说,**Concepts** 是一种编译期的特性,它能够让编译器在编译期时对模板参数进行判断,从而大幅度增强我们在 C++ 中模板编程的体验。使用模板进行编程时候我们经常会遇到各种令人发指的错误,这是因为到目前为止我们始终不能够对模板参数进行检查与限制,例如下面简单的两行代码会造成大量的几乎不可读的编译错误: diff --git a/code/2/2.12.external.template.cpp b/code/2/2.12.external.template.cpp index 93fe927..12262a7 100644 --- a/code/2/2.12.external.template.cpp +++ b/code/2/2.12.external.template.cpp @@ -19,5 +19,5 @@ template class MagicType { int main() { // the >> in template std::vector> matrix; - std::vector2)>> magic; + std::vector2)>> magic; // legal, but not recommended } \ No newline at end of file diff --git a/code/2/2.14.default.template.param.cpp b/code/2/2.14.default.template.param.cpp new file mode 100644 index 0000000..9809742 --- /dev/null +++ b/code/2/2.14.default.template.param.cpp @@ -0,0 +1,18 @@ +// +// 2.4.default.template.param.cpp +// chapter 2 language usability +// modern cpp tutorial +// +// created by changkun at changkun.de +// + +#include + +template +auto add(T x, U y) -> decltype(x+y) { + return x+y; +} + +int main() { + std::cout << add(1, 2) << std::endl; +} \ No newline at end of file diff --git a/code/2/2.15.variadic.template.param.cpp b/code/2/2.15.variadic.template.param.cpp new file mode 100644 index 0000000..709ecfd --- /dev/null +++ b/code/2/2.15.variadic.template.param.cpp @@ -0,0 +1,46 @@ +// +// 2.15.variadic.template.param.cpp +// chapter 2 language usability +// modern cpp tutorial +// +// created by changkun at changkun.de +// + +#include +#include +#include + +// sizeof... +template +void magic(Ts... args) { + std::cout << sizeof...(args) << std::endl; +} + + +// 1 recursive parameter unpack +template +void printf1(T0 value) { + std::cout << value << std::endl; +} +template +void printf1(T value, Ts... args) { + std::cout << value << std::endl; + printf1(args...); +} + +// 2 variadic template parameter unfold +template +void printf2(T0 t0, T... t) { + std::cout << t0 << std::endl; + if constexpr (sizeof...(t) > 0) printf2(t...); +} + +int main() { + magic(); + magic(1); + magic(1,""); + + printf1(1, 2, "123", 1.1); + printf2(1, 2.3, "abc"); + return 0; +} \ No newline at end of file diff --git a/code/2/todo/fold.expression.cpp b/code/2/2.16.fold.expression.cpp similarity index 56% rename from code/2/todo/fold.expression.cpp rename to code/2/2.16.fold.expression.cpp index 2911f75..9926d8e 100644 --- a/code/2/todo/fold.expression.cpp +++ b/code/2/2.16.fold.expression.cpp @@ -1,3 +1,11 @@ +// +// 2.16.fold.expression.cpp +// chapter 2 language usability +// modern cpp tutorial +// +// created by changkun at changkun.de +// + #include template auto sum(T ... t) { diff --git a/code/2/2.18.non.type.template.auto.cpp b/code/2/2.18.non.type.template.auto.cpp new file mode 100644 index 0000000..e6d47fa --- /dev/null +++ b/code/2/2.18.non.type.template.auto.cpp @@ -0,0 +1,18 @@ +// +// 2.18.non.type.template.auto.cpp +// chapter 2 language usability +// modern cpp tutorial +// +// created by changkun at changkun.de +// + +#include + +template void foo() { + std::cout << value << std::endl; + return; +} + +int main() { + foo<10>(); // value is deduced as type int +} \ No newline at end of file diff --git a/code/2/todo/2.6.cpp b/code/2/todo/2.6.cpp deleted file mode 100644 index 8f3c159..0000000 --- a/code/2/todo/2.6.cpp +++ /dev/null @@ -1,76 +0,0 @@ -// -// 2.6.cpp -// modern c++ tutorial -// -// created by changkun at changkun.de -// -// 模板增强 - - -#include -#include -#include - -template class std::vector; // 强行实例化 -extern template class std::vector; // 不在该编译文件中实例化模板 - - -template< typename T, typename U, int value> -class SuckType { -public: - T a; - U b; - SuckType():a(value),b(value){} -}; -// template< typename T> -// typedef SuckType, T, 1> NewType; // 不合法 - -template -using NewType = SuckType; // 合法 - -// 默认模板类型 -template -auto add(T x, U y) -> decltype(x+y) { - return x+y; -} - -// sizeof... -template -void magic(Args... args) { - std::cout << sizeof...(args) << std::endl; -} - - -// 1. 递归解参数包 -template -void printf1(T value) { - std::cout << value << std::endl; -} -template -void printf1(T value, Args... args) { - std::cout << value << std::endl; - printf1(args...); -} -// 2.初始化列表展开解参数包 -template -auto printf2(T value, Args... args) { - std::cout << value << std::endl; - return std::initializer_list{([&] { - std::cout << args << std::endl; - }(), value)...}; -} - -int main() { - - std::vector> wow; // 注意尖括号 - - NewType t; - - magic(); - magic(1); - magic(1,""); - - printf1(1, 2.3, "abc"); - printf2(1, 2.3, "abc"); - return 0; -} diff --git a/code/2/todo/2.xxx.cpp b/code/2/todo/2.xxx.cpp deleted file mode 100644 index c954dd1..0000000 --- a/code/2/todo/2.xxx.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include - -template -void printf(T0 value) { - std::cout << value << std::endl; -} -template -void printf(T value, Args... args) { - std::cout << value << std::endl; - printf(args...); -} - -template -void printf_short(T0 t0, T... t) { - std::cout << t0 << std::endl; - if constexpr (sizeof...(t) > 0) printf(t...); -} -int main() { - printf_short(1, 2, "123", 1.1); - return 0; -} \ No newline at end of file