c++ 17现在特性已经完成,所以不太可能有大的变化。c++ 17提出了数百个建议。
在c++ 17中,哪些特性被添加到了c++中?
当使用支持“c++ 1z”的c++编译器时,当编译器更新到c++ 17时,哪些特性将可用?
c++ 17现在特性已经完成,所以不太可能有大的变化。c++ 17提出了数百个建议。
在c++ 17中,哪些特性被添加到了c++中?
当使用支持“c++ 1z”的c++编译器时,当编译器更新到c++ 17时,哪些特性将可用?
当前回答
语言特点:
模板和通用代码
Template argument deduction for class templates Like how functions deduce template arguments, now constructors can deduce the template arguments of the class http://wg21.link/p0433r2 http://wg21.link/p0620r0 http://wg21.link/p0512r0 template <auto> Represents a value of any (non-type template argument) type. Non-type template arguments fixes template<template<class...>typename bob> struct foo {} ( Folding + ... + expressions ) and Revisions auto x{8}; is an int modernizing using with ... and lists
λ
constexprλ 如果符合条件,lambda是隐式的constexpr 在lambdas中捕获*this [*这]{std:: cout < < < <”是“< <有用< < ' \ n ';}
属性
[[fallthrough]], [[nodiscard]], [[maybe_unused]]属性 [[attributes]]命名空间和enum {erator[[s]]} 使用in属性避免重复属性名称空间。 编译器现在被要求忽略它们无法识别的非标准属性。 c++ 14的措辞允许编译器拒绝未知的作用域属性。
语法清理
内联变量 像内联函数一样 编译器选择实例实例化的位置 弃用静态constexpr重新声明,现在是隐式内联。 名称空间::B 简单static_assert(表达式);没有绳子 没有throw除非throw(), throw()是noexcept(true)。
清洁多回和流量控制
Structured bindings Basically, first-class std::tie with auto Example: const auto [it, inserted] = map.insert( {"foo", bar} ); Creates variables it and inserted with deduced type from the pair that map::insert returns. Works with tuple/pair-likes & std::arrays and relatively flat structs Actually named structured bindings in standard if (init; condition) and switch (init; condition) if (const auto [it, inserted] = map.insert( {"foo", bar} ); inserted) Extends the if(decl) to cases where decl isn't convertible-to-bool sensibly. Generalizing range-based for loops Appears to be mostly support for sentinels, or end iterators that are not the same type as begin iterators, which helps with null-terminated loops and the like. if constexpr Much requested feature to simplify almost-generic code.
Misc
Hexadecimal float point literals Dynamic memory allocation for over-aligned data Guaranteed copy elision Finally! Not in all cases, but distinguishes syntax where you are "just creating something" that was called elision, from "genuine elision". Fixed order-of-evaluation for (some) expressions with some modifications Not including function arguments, but function argument evaluation interleaving now banned Makes a bunch of broken code work mostly, and makes .then on future work. Direct list-initialization of enums Forward progress guarantees (FPG) (also, FPGs for parallel algorithms) I think this is saying "the implementation may not stall threads forever"? u8'U', u8'T', u8'F', u8'8' character literals (string already existed) "noexcept" in the type system __has_include Test if a header file include would be an error makes migrating from experimental to std almost seamless Arrays of pointer conversion fixes inherited constructors fixes to some corner cases (see P0136R0 for examples of behavior changes) aggregate initialization with inheritance. std::launder, type punning, etc
库添加:
数据类型
std::variant<Ts...> Almost-always non-empty last I checked? Tagged union type {awesome|useful} std::optional Maybe holds one of something Ridiculously useful std::any Holds one of anything (that is copyable) std::string_view std::string like reference-to-character-array or substring Never take a string const& again. Also can make parsing a bajillion times faster. "hello world"sv constexpr char_traits std::byte off more than they could chew. Neither an integer nor a character, just data
调用的东西
std::invoke Call any callable (function pointer, function, member pointer) with one syntax. From the standard INVOKE concept. std::apply Takes a function-like and a tuple, and unpacks the tuple into the call. std::make_from_tuple, std::apply applied to object construction is_invocable, is_invocable_r, invoke_result http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0077r2.html http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0604r0.html Deprecates result_of is_invocable<Foo(Args...), R> is "can you call Foo with Args... and get something compatible with R", where R=void is default. invoke_result<Foo, Args...> is std::result_of_t<Foo(Args...)> but apparently less confusing?
文件系统TS v1
(class.path) (class.filesystem.error) (class.file_status) (class.directory_entry) (类。Directory_iterator]和[class.recursive_directory_iterator] (fs.ops.funcs) Fstreams可以用路径打开,也可以用const path::value_type* strings打开。
新算法
for_each_n 减少 transform_reduce exclusive_scan inclusive_scan transform_exclusive_scan transform_inclusive_scan 添加为线程的目的,暴露即使你不使用他们线程
线程
std:: shared_mutex 不定时的,如果你不需要的话可以更有效率。 原子< T >:: is_always_lockfree scoped_lock <互斥…> 当一次锁定多个互斥锁时,可以节省一些std::lock的痛苦。 并行度TS v1 2014年的链接论文,可能已经过时了 std算法的并行版本,以及相关的机器 hardware_ * _interference_size
(部分)库基础TS v1没有覆盖以上或以下
(函数。[alg.search] 一种搜索算法和技术 (pmr) 多态分配器,如用于分配器的std::function 以及一些标准的内存资源。 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0358r1.html 性病:样品,从一个范围内取样?
容器的改进
try_emplace and insert_or_assign gives better guarantees in some cases where spurious move/copy would be bad Splicing for map<>, unordered_map<>, set<>, and unordered_set<> Move nodes between containers cheaply. Merge whole containers cheaply. non-const .data() for string. non-member std::size, std::empty, std::data like std::begin/end Minimal incomplete type support in containers Contiguous iterator "concept" constexpr iterators The emplace family of functions now returns a reference to the created object.
智能指针变化
unique_ptr<T[]>修复和其他unique_ptr调整。 Weak_from_this和一些fixed to shared from this
其他std数据类型改进:
std::tuple的{}构造和其他改进 TriviallyCopyable reference_wrapper,可以提高性能
Misc
C++17 library is based on C11 instead of C99 Reserved std[0-9]+ for future standard libraries destroy(_at|_n), uninitialized_move(_n), uninitialized_value_construct(_n), uninitialized_default_construct(_n) utility code already in most std implementations exposed Special math functions scientists may like them std::clamp() std::clamp( a, b, c ) == std::max( b, std::min( a, c ) ) roughly gcd and lcm std::uncaught_exceptions Required if you want to only throw if safe from destructors std::as_const std::bool_constant A whole bunch of _v template variables std::void_t<T> Surprisingly useful when writing templates std::owner_less<void> like std::less<void>, but for smart pointers to sort based on contents std::chrono polish std::conjunction, std::disjunction, std::negation exposed std::not_fn http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0358r1.html Rules for noexcept within std std::is_contiguous_layout, useful for efficient hashing std::to_chars/std::from_chars, high performance, locale agnostic number conversion; finally a way to serialize/deserialize to human readable formats (JSON & co) std::default_order, indirection over std::less. (breaks ABI of some compilers due to name mangling, removed.) memory_order_consume, added language to prefer use of memory_order_acquire
特征
交换 is_aggregate has_unique_object_representations
弃用
一些C库, < codecvt > Result_of,替换为invoke_result Shared_ptr::unique,它不是很线程安全
自c++ 14以来,Isocpp.org有一个独立的变化列表;它已经被部分掠夺。
当然,TS工作是并行进行的,所以有一些不太成熟的TS将不得不等待下一个迭代。下一个迭代的目标是之前计划的c++ 20,而不是一些传言所说的c++ 19。c++ 1O被避免了。
最初的列表从这个reddit帖子和这个reddit帖子,通过谷歌或从上面的isocpp.org页面添加链接。
从SD-6特性测试列表中掠夺的其他条目。
Clang的功能列表和库功能列表是下一个被掠夺的对象。这似乎并不可靠,因为它是c++ 1z,而不是c++ 17。
这些幻灯片有一些其他地方没有的特色。
虽然“删除了什么”并没有被问到,这里是一个简短的列表,列出了在c++ 17中从c++中删除的一些东西((大部分是?)以前已弃用的):
删除:
注册,关键字保留以供将来使用 bool b;+ + b; 三线图形 如果您仍然需要它们,那么它们现在是源文件编码的一部分,而不是语言的一部分 ios别名 Auto_ptr,旧的<functional>东西,random_shuffle std::function中的分配器
有重写。我不确定这些是否对代码有任何影响,或者它们只是标准中的清理:
尚未纳入上述范畴的文件:
P0505R0 (constexpr chrono) P0418R2 (atomic tweaks) P0512R0 (template argument deduction tweaks) P0490R0 (structured binding tweaks) P0513R0 (changes to std::hash) P0502R0 (parallel exceptions) P0509R1 (updating restrictions on exception handling) P0012R1 (make exception specifications be part of the type system) P0510R0 (restrictions on variants) P0504R0 (tags for optional/variant/any) P0497R0 (shared ptr tweaks) P0508R0 (structured bindings node handles) P0521R0 (shared pointer use count and unique changes?)
规范的变化:
异常规范和抛出表达式
进一步的参考:
论文按年份分组;不全部接受 https://isocpp.org/files/papers/p0636r0.html 这里应该更新为“现有功能的修改”。
其他回答
语言特点:
模板和通用代码
Template argument deduction for class templates Like how functions deduce template arguments, now constructors can deduce the template arguments of the class http://wg21.link/p0433r2 http://wg21.link/p0620r0 http://wg21.link/p0512r0 template <auto> Represents a value of any (non-type template argument) type. Non-type template arguments fixes template<template<class...>typename bob> struct foo {} ( Folding + ... + expressions ) and Revisions auto x{8}; is an int modernizing using with ... and lists
λ
constexprλ 如果符合条件,lambda是隐式的constexpr 在lambdas中捕获*this [*这]{std:: cout < < < <”是“< <有用< < ' \ n ';}
属性
[[fallthrough]], [[nodiscard]], [[maybe_unused]]属性 [[attributes]]命名空间和enum {erator[[s]]} 使用in属性避免重复属性名称空间。 编译器现在被要求忽略它们无法识别的非标准属性。 c++ 14的措辞允许编译器拒绝未知的作用域属性。
语法清理
内联变量 像内联函数一样 编译器选择实例实例化的位置 弃用静态constexpr重新声明,现在是隐式内联。 名称空间::B 简单static_assert(表达式);没有绳子 没有throw除非throw(), throw()是noexcept(true)。
清洁多回和流量控制
Structured bindings Basically, first-class std::tie with auto Example: const auto [it, inserted] = map.insert( {"foo", bar} ); Creates variables it and inserted with deduced type from the pair that map::insert returns. Works with tuple/pair-likes & std::arrays and relatively flat structs Actually named structured bindings in standard if (init; condition) and switch (init; condition) if (const auto [it, inserted] = map.insert( {"foo", bar} ); inserted) Extends the if(decl) to cases where decl isn't convertible-to-bool sensibly. Generalizing range-based for loops Appears to be mostly support for sentinels, or end iterators that are not the same type as begin iterators, which helps with null-terminated loops and the like. if constexpr Much requested feature to simplify almost-generic code.
Misc
Hexadecimal float point literals Dynamic memory allocation for over-aligned data Guaranteed copy elision Finally! Not in all cases, but distinguishes syntax where you are "just creating something" that was called elision, from "genuine elision". Fixed order-of-evaluation for (some) expressions with some modifications Not including function arguments, but function argument evaluation interleaving now banned Makes a bunch of broken code work mostly, and makes .then on future work. Direct list-initialization of enums Forward progress guarantees (FPG) (also, FPGs for parallel algorithms) I think this is saying "the implementation may not stall threads forever"? u8'U', u8'T', u8'F', u8'8' character literals (string already existed) "noexcept" in the type system __has_include Test if a header file include would be an error makes migrating from experimental to std almost seamless Arrays of pointer conversion fixes inherited constructors fixes to some corner cases (see P0136R0 for examples of behavior changes) aggregate initialization with inheritance. std::launder, type punning, etc
库添加:
数据类型
std::variant<Ts...> Almost-always non-empty last I checked? Tagged union type {awesome|useful} std::optional Maybe holds one of something Ridiculously useful std::any Holds one of anything (that is copyable) std::string_view std::string like reference-to-character-array or substring Never take a string const& again. Also can make parsing a bajillion times faster. "hello world"sv constexpr char_traits std::byte off more than they could chew. Neither an integer nor a character, just data
调用的东西
std::invoke Call any callable (function pointer, function, member pointer) with one syntax. From the standard INVOKE concept. std::apply Takes a function-like and a tuple, and unpacks the tuple into the call. std::make_from_tuple, std::apply applied to object construction is_invocable, is_invocable_r, invoke_result http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0077r2.html http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0604r0.html Deprecates result_of is_invocable<Foo(Args...), R> is "can you call Foo with Args... and get something compatible with R", where R=void is default. invoke_result<Foo, Args...> is std::result_of_t<Foo(Args...)> but apparently less confusing?
文件系统TS v1
(class.path) (class.filesystem.error) (class.file_status) (class.directory_entry) (类。Directory_iterator]和[class.recursive_directory_iterator] (fs.ops.funcs) Fstreams可以用路径打开,也可以用const path::value_type* strings打开。
新算法
for_each_n 减少 transform_reduce exclusive_scan inclusive_scan transform_exclusive_scan transform_inclusive_scan 添加为线程的目的,暴露即使你不使用他们线程
线程
std:: shared_mutex 不定时的,如果你不需要的话可以更有效率。 原子< T >:: is_always_lockfree scoped_lock <互斥…> 当一次锁定多个互斥锁时,可以节省一些std::lock的痛苦。 并行度TS v1 2014年的链接论文,可能已经过时了 std算法的并行版本,以及相关的机器 hardware_ * _interference_size
(部分)库基础TS v1没有覆盖以上或以下
(函数。[alg.search] 一种搜索算法和技术 (pmr) 多态分配器,如用于分配器的std::function 以及一些标准的内存资源。 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0358r1.html 性病:样品,从一个范围内取样?
容器的改进
try_emplace and insert_or_assign gives better guarantees in some cases where spurious move/copy would be bad Splicing for map<>, unordered_map<>, set<>, and unordered_set<> Move nodes between containers cheaply. Merge whole containers cheaply. non-const .data() for string. non-member std::size, std::empty, std::data like std::begin/end Minimal incomplete type support in containers Contiguous iterator "concept" constexpr iterators The emplace family of functions now returns a reference to the created object.
智能指针变化
unique_ptr<T[]>修复和其他unique_ptr调整。 Weak_from_this和一些fixed to shared from this
其他std数据类型改进:
std::tuple的{}构造和其他改进 TriviallyCopyable reference_wrapper,可以提高性能
Misc
C++17 library is based on C11 instead of C99 Reserved std[0-9]+ for future standard libraries destroy(_at|_n), uninitialized_move(_n), uninitialized_value_construct(_n), uninitialized_default_construct(_n) utility code already in most std implementations exposed Special math functions scientists may like them std::clamp() std::clamp( a, b, c ) == std::max( b, std::min( a, c ) ) roughly gcd and lcm std::uncaught_exceptions Required if you want to only throw if safe from destructors std::as_const std::bool_constant A whole bunch of _v template variables std::void_t<T> Surprisingly useful when writing templates std::owner_less<void> like std::less<void>, but for smart pointers to sort based on contents std::chrono polish std::conjunction, std::disjunction, std::negation exposed std::not_fn http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0358r1.html Rules for noexcept within std std::is_contiguous_layout, useful for efficient hashing std::to_chars/std::from_chars, high performance, locale agnostic number conversion; finally a way to serialize/deserialize to human readable formats (JSON & co) std::default_order, indirection over std::less. (breaks ABI of some compilers due to name mangling, removed.) memory_order_consume, added language to prefer use of memory_order_acquire
特征
交换 is_aggregate has_unique_object_representations
弃用
一些C库, < codecvt > Result_of,替换为invoke_result Shared_ptr::unique,它不是很线程安全
自c++ 14以来,Isocpp.org有一个独立的变化列表;它已经被部分掠夺。
当然,TS工作是并行进行的,所以有一些不太成熟的TS将不得不等待下一个迭代。下一个迭代的目标是之前计划的c++ 20,而不是一些传言所说的c++ 19。c++ 1O被避免了。
最初的列表从这个reddit帖子和这个reddit帖子,通过谷歌或从上面的isocpp.org页面添加链接。
从SD-6特性测试列表中掠夺的其他条目。
Clang的功能列表和库功能列表是下一个被掠夺的对象。这似乎并不可靠,因为它是c++ 1z,而不是c++ 17。
这些幻灯片有一些其他地方没有的特色。
虽然“删除了什么”并没有被问到,这里是一个简短的列表,列出了在c++ 17中从c++中删除的一些东西((大部分是?)以前已弃用的):
删除:
注册,关键字保留以供将来使用 bool b;+ + b; 三线图形 如果您仍然需要它们,那么它们现在是源文件编码的一部分,而不是语言的一部分 ios别名 Auto_ptr,旧的<functional>东西,random_shuffle std::function中的分配器
有重写。我不确定这些是否对代码有任何影响,或者它们只是标准中的清理:
尚未纳入上述范畴的文件:
P0505R0 (constexpr chrono) P0418R2 (atomic tweaks) P0512R0 (template argument deduction tweaks) P0490R0 (structured binding tweaks) P0513R0 (changes to std::hash) P0502R0 (parallel exceptions) P0509R1 (updating restrictions on exception handling) P0012R1 (make exception specifications be part of the type system) P0510R0 (restrictions on variants) P0504R0 (tags for optional/variant/any) P0497R0 (shared ptr tweaks) P0508R0 (structured bindings node handles) P0521R0 (shared pointer use count and unique changes?)
规范的变化:
异常规范和抛出表达式
进一步的参考:
论文按年份分组;不全部接受 https://isocpp.org/files/papers/p0636r0.html 这里应该更新为“现有功能的修改”。