# C++17_new_features_introduction_and_demo **Repository Path**: kennethyu/cpp17_new_features_introduction_and_demo ## Basic Information - **Project Name**: C++17_new_features_introduction_and_demo - **Description**: introduction of C++17 key new features, with demo code. include: 1. core language key new features. 2. STL key new features. - **Primary Language**: C++ - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-06-08 - **Last Updated**: 2022-06-26 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # C++17 关键新特性介绍及代码讲解 (1) --- 前言及目录 --- ## 前言 ![CPP STANDARD](resources/cpp_logo.png "CPP STANDARD") C++最早的版本可以追溯到 1998 年,即C++98; 从 C++11 开始,ISO C++ 委员会(也叫做 WG21 ),保持每三年一个 ISO 标准版本的发布频率: | 年份 | C++版本名称 | ISO标准号 | | :---: | :---: | :---: | | 1998 | C++98 | ISO/IEC 14882:1998 | | 2003 | C++03 | ISO/IEC 14882:2003 | | 2011 | C++11 | ISO/IEC 14882:2011 | | 2014 | C++14 | ISO/IEC 14882:2014 | | 2017 | C++17 | ISO/IEC 14882:2017 | | 2020 | C++20 | ISO/IEC 14882:2020 | | 2023 | C++23 | in-progress | C++17,也就是 2017 年发布成为 [ISO/IEC 14882:2017](https://www.iso.org/standard/79358.html) 标准的版本, 我们在这里将 C++17 的关键新特性, 选取一些显著的、常用的内容,简要讲明这些特性是什么、有什么好处、要解决什么问题,同时配有 demo code 和 CMakeList,你可以直接 build 运行、修改源代码等,观察运行结果。 所有示例源代码均存放于 gitee.com 上的公开代码仓库,路径:[[cpp17_new_features_introduction_and_demo]](https://gitee.com/kennethyu/cpp17_new_features_introduction_and_demo) C++17 是否已经成为项目开发的主流 C++ 版本? 我们看看 Google 当前(2022年6月10日)使用的 C++ 版本: 引用自 [ [Google C++ Style Guide] ](https://google.github.io/styleguide/cppguide.html#C++_Version) : > ### C++ Version >currently code should target ***C++17***, i.e., should not use C++2x features, with the exception of designated initializers. 需要说明的是,目前一些知名的大型开源 C++ 项目,主流还是 C++11/C++14, 比如: pytorch 的 C++ 源代码使用 C++14; ROS2 Galaxy 的 C++ 源代码使用C++11/C++14(未来有升级到C++17的计划); 百度 Apollo 的 C++ 源代码使用 C++11。 同时,我们需要注意各主流编译器,比如gcc、llvm等,对 C++17 各特性的支持情况, 参见: [Compiler support for C++17](https://en.cppreference.com/w/cpp/compiler_support/17) demo 代码用到的开发和运行环境如下: >CPU: AMD Ryzen 7 5800H > >操作系统: ubuntu 20.04 > >编译器: gcc 10.3.0 --- ## 特性讲解目录索引 _每个目录项均为超链接,可跳转至详细的介绍和代码示例_ ### 核心语法更新 #### [1. New auto rules for direct-list-initialization](https://gitee.com/kennethyu/cpp17_new_features_introduction_and_demo/blob/master/auto_bound_list_initialization/README.md) 对于使用 brace-list ( 即花括弧 ```{ }``` ) 进行初始化的 ```auto``` 变量,更新了 ```auto``` 变量类型的推导规则,更新后的规则更加清晰。 #### [2 typename in a template template parameter](https://gitee.com/kennethyu/cpp17_new_features_introduction_and_demo/blob/master/typename_in_a_template_template_parameter/README.md) 在声明 template template parameter 时,允许使用使用关键字 ```typename```。 #### [3. nested namespace](https://gitee.com/kennethyu/cpp17_new_features_introduction_and_demo/blob/master/netsted_namespace/README.md) 在定义 namespace 时,允许通过添加 qualified name 的方式,一次定义多层的、嵌套式 namespace 。 #### [4. attributes for namespaces and enumerators](https://gitee.com/kennethyu/cpp17_new_features_introduction_and_demo/blob/master/attributes_for_namespaces_and_enumerators/README.md) 在定义 ```namespace``` 、```enum``` 时,允许指定 attributes。 这里的 attributes 指: 具体所使用的C++编译器所提供的语言扩展属性。 #### [5. fold expression](https://gitee.com/kennethyu/cpp17_new_features_introduction_and_demo/blob/master/fold_expression/README.md) fold expression 是将 parameter pack 进行递归展开、并通过二元操作符 (binary operator) 进行运算的一种表达式。 #### [6. lambda capture of '*this' by value](https://gitee.com/kennethyu/cpp17_new_features_introduction_and_demo/blob/master/lambda_capture_of_this_by_value/README.md) 在定义 lambda 的 captures 时,可以通过 ```[*this]``` 这种形式,将当前 this 所指向的 object,以 value-copy 的方式拷贝下来,并在 lambda 的表达式中使用拷贝得到的 object 成员变量值。 #### 未完待续... ### STL库的新增功能 #### 未完待续...