C 17现在功能齐全,因此不太可能经历大的变化 . 为C 17提出了数百项提案 .
在C 17中将哪些功能添加到C中?
当使用支持“C 1z”的C编译器时,当编译器更新到C 17时,哪些功能可用?
Template argument deduction for class templates
与函数如何推导模板参数一样,现在构造函数可以推导出类的模板参数
http://wg21.link/p0433r2 http://wg21.link/p0620r0 http://wg21.link/p0512r0
template <auto>
表示任何(非类型模板参数)类型的值 .
Non-type template arguments fixes
template<template<class...>typename bob> struct foo {}
( Folding + ... + expressions )和Revisions
auto x{8}; is an int
modernizing using with ... and lists
constexpr lambdas
Lambdas如果符合条件,则隐含着constexpr
Capturing *this in lambdas
[*this]{ std::cout << could << " be " << useful << '\n'; }
[[fallthrough]],[[nodiscard]],_326651_属性
[[attributes]] on namespaces and enum { erator[[s]] }
using in attributes以避免重复属性命名空间 .
编译器现在required to ignore non-standard attributes they don't recognize .
C 14措辞允许编译器拒绝未知的范围属性 .
Inline variables
与内联函数类似
编译器选择实例实例化的位置
Deprecate static constexpr redeclaration,现在隐式内联 .
namespace A::B
简单static_assert(expression);没有字符串
no throw unless throw()和 throw() 是 noexcept(true) .
throw()
noexcept(true)
Structured bindings
基本上,一流的 std::tie 与 auto
std::tie
auto
示例:
const auto [it, inserted] = map.insert( {"foo", bar} );
使用 pair 返回的 pair 中的推导类型创建变量 it 和 inserted .
pair
it
inserted
使用元组/对喜欢& std::array 和相对平坦的结构
std::array
实际上在标准中命名为structured bindings
if (init; condition) and switch (init; condition)
if (const auto [it, inserted] = map.insert( {"foo", bar} ); inserted)
将 if(decl) 扩展到 decl 不能明智地转换为bool的情况 .
if(decl)
decl
Generalizing range-based for loops
似乎主要支持哨兵,或者结束与开始迭代器类型不同的迭代器,这有助于使用以null结尾的循环等 .
if constexpr
许多要求的功能,以简化几乎通用的代码 .
Hexadecimal float point literals
Dynamic memory allocation for over-aligned data
Guaranteed copy elision
终于来了!
并非在所有情况下,但从"genuine elision"区分您所谓的elision的语法"just creating something" .
Fixed order-of-evaluation for (some) expressions有一些modifications
不包括函数参数,但现在禁止函数参数评估交错
主要使一堆破碎的代码工作,并在未来的工作中使 .then .
.then
Direct list-initialization of enums
远期进度保证(FPG)(另外,FPGs for parallel algorithms)
我想这是在说"the implementation may not stall threads forever"?
u8'U', u8'T', u8'F', u8'8'字符文字(字符串已存在)
"noexcept" in the type system
__has_include
测试头文件包含是否为错误
使得从实验迁移到std几乎是无缝的
Arrays of pointer conversion fixes
inherited constructors修复了一些极端情况(有关行为更改的示例,请参阅P0136R0)
aggregate initialization with inheritance .
std::launder, type punning, etc
std::variant<Ts...>
我最后检查几乎总是非空的?
标记的联合类型
{awesome | useful}
std::optional
也许拥有一件事
非常有用
std::any
持有任何东西(可复制)
std::string_view
std::string like reference-to-character-array或substring
std::string
永远不要再拿 string const& . 也可以使解析速度提高5倍 .
string const&
"hello world"sv
constexpr char_traits
std::byte关闭超过他们可以咀嚼的 .
既不是整数也不是字符,只是数据
std::invoke
使用一种语法调用任何可调用的(函数指针,函数,成员指针) . 从标准的INVOKE概念 .
std::apply
采用类似函数和元组,并将元组解包到调用中 .
std::make_from_tuple, std::apply 应用于对象构造
is_invocable , is_invocable_r , invoke_result
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
弃用 result_of
result_of
is_invocable<Foo(Args...), R> 是“你可以用 Args... 调用 Foo 并获得与 R 兼容的东西”,其中 R=void 是默认值 .
is_invocable<Foo(Args...), R>
Args...
Foo
R
R=void
invoke_result<Foo, Args...> 是 std::result_of_t<Foo(Args...)> 但显然不那么混乱?
invoke_result<Foo, Args...>
std::result_of_t<Foo(Args...)>
[class.path]
[class.filesystem.error]
[class.file_status]
[class.directory_entry]
[class.directory_iterator]和[class.recursive_directory_iterator]
[fs.ops.funcs]
fstreams can be opened with paths, as well as with const path::value_type* strings.
for_each_n
reduce
transform_reduce
exclusive_scan
inclusive_scan
transform_exclusive_scan
transform_inclusive_scan
添加用于线程目的,即使您没有使用它们也会暴露
std::shared_mutex
不定时,如果你不需要它可以更有效率 .
atomic<T> ::is_always_lockfree
atomic<T>
scoped_lock<Mutexes...>
保存一些 std::lock 一次锁定多个互斥锁时疼痛 .
std::lock
Parallelism TS v1
2014年的链接文件可能已过期
std 算法和相关机器的并行版本
std
hardware_*_interference_size
[func.searchers]和[alg.search]
搜索算法和技术
[pmr]
多态分配器,如 std::function 用于分配器
std::function
还有一些standard memory resources to go with it .
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0358r1.html
std::sample,从一个范围采样?
在虚假移动/复制不好的某些情况下,
可以提供更好的保证
Splicing for map<>, unordered_map<>, set<>, and unordered_set<>
便宜地在容器之间移动节点 .
便宜地合并整个容器 .
非const .data() for string .
非会员std::size, std::empty, std::data
喜欢 std::begin / end
std::begin
end
Minimal incomplete type support in containers
Contiguous iterator "concept"
constexpr iterators
emplace 系列函数now returns a reference to the created object .
emplace
unique_ptr<T[]> fixes和其他unique_ptr调整 .
weak_from_this和一些固定为共享的
{} construction of std::tuple and other improvements
TriviallyCopyable reference_wrapper,可以提升性能
C 17库基于C11 instead of C99
保留 std[0-9]+ for future standard libraries
std[0-9]+
destroy(_at|_n), uninitialized_move(_n), uninitialized_value_construct(_n), uninitialized_default_construct(_n)
大多数 std 实现中已经公开了
实用程序代码
Special math functions
科学家可能喜欢他们
std::clamp()
std::clamp( a, b, c ) == std::max( b, std::min( a, c ) ) 粗略
std::clamp( a, b, c ) == std::max( b, std::min( a, c ) )
gcd and lcm
std::uncaught_exceptions
如果您只想从析构函数中安全抛出,则必需
std::as_const
std::bool_constant
A whole bunch of _v template variables
std::void_t<T>
在编写模板时非常有用
std::owner_less<void>
喜欢 std::less<void> ,但是对于基于内容排序的智能指针
std::less<void>
std::chrono polish
std::conjunction, std::disjunction, std::negation暴露
std::not_fn
Rules for noexcept within std
std::is_contiguous_layout,对高效散列很有用
std::to_chars/std::from_chars,高性能,语言环境不可知数转换;最后一种序列化/反序列化为人类可读格式的方法(JSON&co)
std :: default_order,间接超过std :: less . (breaks ABI of some compilers由于名称损坏,已删除 . )
swap
is_aggregate
has_unique_object_representations
Some C libraries,
<codecvt>
memory_order_consume
result_of,替换为 invoke_result
shared_ptr::unique,它不是非常线程安全的
Isocpp.org has自C 14以来有一个独立的变化清单;它已被部分掠夺 .
自然地,TS工作并行继续,因此有一些TS不够成熟,必须等待下一次迭代 . 下一次迭代的目标是先前计划的C 20,而不是C 19,正如一些谣言所暗示的那样 . 已经避免了C 1O .
初始列表取自this reddit post和this reddit post,链接通过谷歌搜索或从上面的isocpp.org页面添加 .
从SD-6功能测试列表中掠夺的其他条目 .
其次是clang's feature list和library feature list被掠夺 . 这似乎不可靠,因为它是C 1z,而不是C 17 .
these slides其他地方缺少一些功能 .
虽然没有问“删除了什么”,但这里是C 17中从C中删除的一些事情的简短列表((主要是?)以前弃用的):
register,保留供将来使用的关键字
bool b; ++b;
trigraphs
如果您仍然需要它们,它们现在是源文件编码的一部分,而不是语言的一部分
ios aliases
auto_ptr, old <functional> stuff, random_shuffle
allocators in std::function
有重写 . 我不确定这些是否对代码有任何影响,或者它们是否只是标准中的清理:
P0505R0(constexpr chrono)
P0418R2(原子调整)
P0512R0(模板参数扣除调整)
P0490R0(结构化绑定调整)
P0513R0(更改为 std::hash )
std::hash
P0502R0(并行异常)
P0509R1(更新异常处理的限制)
P0012R1(使异常规范成为类型系统的一部分)
P0510R0(对变种的限制)
P0504R0(可选/变体/任意标签)
P0497R0(共享ptr调整)
P0508R0(结构化绑定节点句柄)
P0521R0(共享指针使用计数和唯一更改?)
papers grouped by year; not all accepted
https://isocpp.org/files/papers/p0636r0.html
应该更新到"Modifications to existing features"这里 .
1 回答
语言特点:
模板和通用代码
Template argument deduction for class templates
与函数如何推导模板参数一样,现在构造函数可以推导出类的模板参数
http://wg21.link/p0433r2 http://wg21.link/p0620r0 http://wg21.link/p0512r0
template <auto>
表示任何(非类型模板参数)类型的值 .
Non-type template arguments fixes
template<template<class...>typename bob> struct foo {}
( Folding + ... + expressions )和Revisions
auto x{8}; is an int
modernizing using with ... and lists
Lambda
constexpr lambdas
Lambdas如果符合条件,则隐含着constexpr
Capturing *this in lambdas
[*this]{ std::cout << could << " be " << useful << '\n'; }
属性
[[fallthrough]],[[nodiscard]],_326651_属性
[[attributes]] on namespaces and enum { erator[[s]] }
using in attributes以避免重复属性命名空间 .
编译器现在required to ignore non-standard attributes they don't recognize .
C 14措辞允许编译器拒绝未知的范围属性 .
语法清理
Inline variables
与内联函数类似
编译器选择实例实例化的位置
Deprecate static constexpr redeclaration,现在隐式内联 .
namespace A::B
简单static_assert(expression);没有字符串
no throw unless throw()和
throw()
是noexcept(true)
.清洁多回程和流量控制
Structured bindings
基本上,一流的
std::tie
与auto
示例:
const auto [it, inserted] = map.insert( {"foo", bar} );
使用
pair
返回的pair
中的推导类型创建变量it
和inserted
.使用元组/对喜欢&
std::array
和相对平坦的结构实际上在标准中命名为structured bindings
if (init; condition) and switch (init; condition)
if (const auto [it, inserted] = map.insert( {"foo", bar} ); inserted)
将
if(decl)
扩展到decl
不能明智地转换为bool的情况 .Generalizing range-based for loops
似乎主要支持哨兵,或者结束与开始迭代器类型不同的迭代器,这有助于使用以null结尾的循环等 .
if constexpr
许多要求的功能,以简化几乎通用的代码 .
其他
Hexadecimal float point literals
Dynamic memory allocation for over-aligned data
Guaranteed copy elision
终于来了!
并非在所有情况下,但从"genuine elision"区分您所谓的elision的语法"just creating something" .
Fixed order-of-evaluation for (some) expressions有一些modifications
不包括函数参数,但现在禁止函数参数评估交错
主要使一堆破碎的代码工作,并在未来的工作中使
.then
.Direct list-initialization of enums
远期进度保证(FPG)(另外,FPGs for parallel algorithms)
我想这是在说"the implementation may not stall threads forever"?
u8'U', u8'T', u8'F', u8'8'字符文字(字符串已存在)
"noexcept" in the type system
__has_include
测试头文件包含是否为错误
使得从实验迁移到std几乎是无缝的
Arrays of pointer conversion fixes
inherited constructors修复了一些极端情况(有关行为更改的示例,请参阅P0136R0)
aggregate initialization with inheritance .
std::launder, type punning, etc
图书馆新增内容:
数据类型
std::variant<Ts...>
我最后检查几乎总是非空的?
标记的联合类型
{awesome | useful}
std::optional
也许拥有一件事
非常有用
std::any
持有任何东西(可复制)
std::string_view
std::string
like reference-to-character-array或substring永远不要再拿
string const&
. 也可以使解析速度提高5倍 ."hello world"sv
constexpr char_traits
std::byte关闭超过他们可以咀嚼的 .
既不是整数也不是字符,只是数据
调用东西
std::invoke
使用一种语法调用任何可调用的(函数指针,函数,成员指针) . 从标准的INVOKE概念 .
std::apply
采用类似函数和元组,并将元组解包到调用中 .
std::make_from_tuple,
std::apply
应用于对象构造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
弃用
result_of
is_invocable<Foo(Args...), R>
是“你可以用Args...
调用Foo
并获得与R
兼容的东西”,其中R=void
是默认值 .invoke_result<Foo, Args...>
是std::result_of_t<Foo(Args...)>
但显然不那么混乱?文件系统TS v1
[class.path]
[class.filesystem.error]
[class.file_status]
[class.directory_entry]
[class.directory_iterator]和[class.recursive_directory_iterator]
[fs.ops.funcs]
fstreams can be opened with paths, as well as with const path::value_type* strings.
新算法
for_each_n
reduce
transform_reduce
exclusive_scan
inclusive_scan
transform_exclusive_scan
transform_inclusive_scan
添加用于线程目的,即使您没有使用它们也会暴露
线程
std::shared_mutex
不定时,如果你不需要它可以更有效率 .
atomic<T>
::is_always_lockfreescoped_lock<Mutexes...>
保存一些
std::lock
一次锁定多个互斥锁时疼痛 .Parallelism TS v1
2014年的链接文件可能已过期
std
算法和相关机器的并行版本hardware_*_interference_size
(部分)库基础知识TS v1未在上文或下文中介绍
[func.searchers]和[alg.search]
搜索算法和技术
[pmr]
多态分配器,如
std::function
用于分配器还有一些standard memory resources to go with it .
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0358r1.html
std::sample,从一个范围采样?
集装箱改进
在虚假移动/复制不好的某些情况下,
可以提供更好的保证
Splicing for map<>, unordered_map<>, set<>, and unordered_set<>
便宜地在容器之间移动节点 .
便宜地合并整个容器 .
非const .data() for string .
非会员std::size, std::empty, std::data
喜欢
std::begin
/end
Minimal incomplete type support in containers
Contiguous iterator "concept"
constexpr iterators
emplace
系列函数now returns a reference to the created object .智能指针变化
unique_ptr<T[]> fixes和其他unique_ptr调整 .
weak_from_this和一些固定为共享的
其他std数据类型改进:
{} construction of std::tuple and other improvements
TriviallyCopyable reference_wrapper,可以提升性能
其他
C 17库基于C11 instead of C99
保留
std[0-9]+
for future standard librariesdestroy(_at|_n), uninitialized_move(_n), uninitialized_value_construct(_n), uninitialized_default_construct(_n)
大多数
std
实现中已经公开了实用程序代码
Special math functions
科学家可能喜欢他们
std::clamp()
std::clamp( a, b, c ) == std::max( b, std::min( a, c ) )
粗略gcd and lcm
std::uncaught_exceptions
如果您只想从析构函数中安全抛出,则必需
std::as_const
std::bool_constant
A whole bunch of _v template variables
std::void_t<T>
在编写模板时非常有用
std::owner_less<void>
喜欢
std::less<void>
,但是对于基于内容排序的智能指针std::chrono polish
std::conjunction, std::disjunction, std::negation暴露
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,对高效散列很有用
std::to_chars/std::from_chars,高性能,语言环境不可知数转换;最后一种序列化/反序列化为人类可读格式的方法(JSON&co)
std :: default_order,间接超过std :: less . (breaks ABI of some compilers由于名称损坏,已删除 . )
特质
swap
is_aggregate
has_unique_object_representations
已弃用
Some C libraries,
<codecvt>
memory_order_consume
result_of,替换为
invoke_result
shared_ptr::unique,它不是非常线程安全的
Isocpp.org has自C 14以来有一个独立的变化清单;它已被部分掠夺 .
自然地,TS工作并行继续,因此有一些TS不够成熟,必须等待下一次迭代 . 下一次迭代的目标是先前计划的C 20,而不是C 19,正如一些谣言所暗示的那样 . 已经避免了C 1O .
初始列表取自this reddit post和this reddit post,链接通过谷歌搜索或从上面的isocpp.org页面添加 .
从SD-6功能测试列表中掠夺的其他条目 .
其次是clang's feature list和library feature list被掠夺 . 这似乎不可靠,因为它是C 1z,而不是C 17 .
these slides其他地方缺少一些功能 .
虽然没有问“删除了什么”,但这里是C 17中从C中删除的一些事情的简短列表((主要是?)以前弃用的):
删除:
register,保留供将来使用的关键字
bool b; ++b;
trigraphs
如果您仍然需要它们,它们现在是源文件编码的一部分,而不是语言的一部分
ios aliases
auto_ptr, old <functional> stuff, random_shuffle
allocators in std::function
有重写 . 我不确定这些是否对代码有任何影响,或者它们是否只是标准中的清理:
尚未整合到上面的论文:
P0505R0(constexpr chrono)
P0418R2(原子调整)
P0512R0(模板参数扣除调整)
P0490R0(结构化绑定调整)
P0513R0(更改为
std::hash
)P0502R0(并行异常)
P0509R1(更新异常处理的限制)
P0012R1(使异常规范成为类型系统的一部分)
P0510R0(对变种的限制)
P0504R0(可选/变体/任意标签)
P0497R0(共享ptr调整)
P0508R0(结构化绑定节点句柄)
P0521R0(共享指针使用计数和唯一更改?)
规格变化:
进一步参考:
papers grouped by year; not all accepted
https://isocpp.org/files/papers/p0636r0.html
应该更新到"Modifications to existing features"这里 .