C ++ 17的新特性是什么? [英] What are the new features in C++17?
问题描述
C ++ 17现在功能齐全,因此不太可能遇到大的更改。已经为C ++ 17提出了数百个提案。
哪些功能在C ++ 17中添加到C ++?
当使用支持C ++ 1z的C ++编译器时,当编译器更新为C ++ 17时,哪些功能将可用?
语言特性:
模板和通用代码
-
- 就像函数如何推导模板参数一样,现在构造函数可以推导出类模板参数
-
- 表示任何(非类型模板参数)类型的值
-
a href =http://open-std.org/JTC1/SC22/WG21/docs/papers/2016/p0128r1.html =nofollow> constexpr if
Lambda
-
- Lambdas如果符合资格则默认为constexpr
-
-
[* this] {std :: cout<可以< be<<有用的< '\\\
'; }
-
属性
-
编译器现在需要忽略他们无法识别的非标准属性。
- C ++ 14的措辞允许编译器拒绝未知范围的属性。
语法cleanup
-
- 与内联函数类似
- 编译器选择实例实例化的位置?
-
简单
static_assert(expression);
,不含字符串
清洁剂多返回和流控制
-
- 基本上,第一类
std :: tie
与
自动
- 示例:
-
const auto [it,inserted] = map.insert({foo,bar});
- 创建变量
it 和
从
返回。对中推导出类型
$ c> map :: insert
-
- 使用tuple / pair-
std :: array
s和相对扁平的结构
- 基本上,第一类
-
if(init; condition)
和switch(init; condition)
-
if(const auto [it,inserted] = map.insert({foo,bar}); insert)
-
if(decl)
扩展到decl
不可转换为bool的情况
-
-
- 似乎大多数是支持哨兵,或与开始迭代器类型不同的结束迭代器,这有助于以null结尾的循环等。
Misc
-
- 最后!
- 不是在所有情况下,但在所有情况下,但在distingushes语法,你是只是创造的东西,被称为elision,从真正的elision。
-
- 不包括函数参数,但函数参数评估交叉已禁止
- 使大量破碎的代码工作,并使
.then
在未来的工作。
-
前进进度保证(FPG)(同时,并行算法的FPG )
- 我认为这是说实现可能不会停止线程永远?
-
u8'U',u8'T',u8'F',u8'8'
字符文字。 -
- 测试头文件是否包含错误
- 几乎从实验迁移到std无缝
-
继承了构造函数修复了一些角点。
库添加:
数据类型
b $ b
-
- $ b
$ b $ b b $ b
- 可能持有某物
- 太可用了
a href =http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4480.html#any =nofollow> std :: any
- 持有其中任何一项(可复制)
-
std :: string
like reference-to-character-array或substring - 不要使用
string const&
。
调用内容
-
std :: invoke
- 函数指针,函数,成员指针)。从标准的INVOKE概念。
-
std :: apply
- 使用函数式和元组,并将元组解包到调用中。
-
std :: is_callable
-
std :: make_from_tuple
,std :: apply
应用于对象构造
文件系统TS v1
-
a href =http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0218r0.html#class-path =nofollow>
[class .path]
-
[class.directory_iterator]
和[class.recursive_directory_iterator]
-
在撰写本文时,小写:没有简单和可移植的方式来打开一个文件。希望稍后修复。
线程
-
- Untimed, =http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0152r1.html =nofollow>
:: is_always_lockfree
-
- 在每次锁定多个互斥体时节省一些
std :: lock
。
- 在每次锁定多个互斥体时节省一些
-
- 2014年的链接论文可能已过期
-
std
算法和相关机制的并行版本
(部分)图书基础知识TS v1 以上或以下不包括
-
[func.searchers]
和[alg.search]
- 搜索算法和技巧
-
- 多边形分配器,例如
std :: function
>
- 和一些标准内存资源。
- 多边形分配器,例如
容器改进
-
try_emplace
和insert_or_assign
- $
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0083r3.pdf =nofollow>拼接 - 在容器之间廉价移动节点。
-
non-const
.data()
。 / p> -
非会员
std :: size
,std :: empty
,std :: data
- like
/ code> /
end
- like
-
emplace
家庭功能现在返回对创建对象的引用。
地图<>
, unordered_map
, set<>
和 unordered_set<
智能指针变更
-
unique_ptr< T []>
修复和其他unique_ptr
tweaks。 -
weak_from_this
和一些固定为分享的
其他 std
数据类型改进:
Misc
-
C ++ 17库基于 C11而非C99
-
- 实用程式程式码已包含在大多数
std
>
- 实用程式程式码已包含在大多数
- 特殊数学函数
- 科学家可能喜欢它们
-
std :: clamp()
-
std :: clamp(a,b,c)== std :: max(b,std :: min(a,c))
大致
-
-
std :: uncaught_exceptions
- 如果您只想扔掉, >
- 如果您只想扔掉, >
-
std :: as_const
-
std :: bool_constant
- A整个
_v
模板变量 -
std :: void_t< T>
- 撰写范本时非常有用
-
std :: owner_less< void>
- 喜欢
std :: less< void>
基于内容排序
- 喜欢
-
std :: chrono
polish -
std :: conjunction
,std :: disjunction
,std :: negation
-
std :: not_fn
-
std
中的noexcept规则
- std :: is_contiguous_layout ,对于高效散列很有用
- std :: to_chars / std :: from_chars ,高性能,区域设置不可知数转换;最后一种将序列化/反序列化为人类可读格式(JSON& co)的方法
- std :: default_order ,
std :: less
的间接。
Isocpp.org < a>一个更全面的文件列表,其中一些我为上面抢救。在这一点上,它是不是最新的接受到C ++ 17。
自然TS工作并行继续,所以有一些TS不是 - - 成熟的,将不得不等待下一次迭代。下一次迭代的目标是以前计划的C ++ 20,而不是一些谣言暗示的C ++ 19。已避免C ++1Ω。
从 SD-6 功能测试列表。
clang的功能列表和图书馆功能列表旁边是被抢。这似乎不可靠,因为它是C ++ 1z,而不是C + + 17。
这些幻灯片在其他地方缺少某些功能。
While 什么被删除没有问,这里是一个简单的几个东西((多数?)以前弃用),在C ++ 17从C ++中删除:
< h1>已移除:
-
注册
,关键字保留以备将来使用 -
bool b; ++ b;
- trigraphs
- 如果您仍然需要他们,他们现在是您的一部分源文件编码,而不是语言的一部分
- ios别名
- auto_ptr,old
< functional>
stuff,random_shuffle
-
std :: function
中的分配器
有改动。我不确定这些是否对代码有任何影响,或者如果他们只是在标准清理:
规格更改:
C++17 is now feature complete, so unlikely to experience large changes. Hundreds of proposals were put forward for C++17.
Which of those features were added to C++ in C++17?
When using a C++ compiler that supports "C++1z", which of those features are going to be available when the compiler updates to C++17?
Language features:
Templates and Generic Code
Template argument deduction for class templates
- Like how functions deduce template arguments, now constructors can deduce the template arguments of the class
-
- Represents a value of any (non-type template argument) type
-
- Much requested feature to simplify almost-generic code
Lambda
-
- Lambdas are implicitly constexpr if they qualify
-
[*this]{ std::cout << could << " be " << useful << '\n'; }
Attributes
using
in attributes to avoid having to repeat an attribute namespace.Compilers are now required to ignore non-standard attributes they don't recognize.
- The C++14 wording allowed compilers to reject unknown scoped attributes.
Syntax cleanup
-
- Like inline functions
- Compiler picks where the instance is instantiated?
Simple
static_assert(expression);
with no string
Cleaner multi-return and flow control
-
- Basically, first-class
std::tie
withauto
- Example:
const auto [it, inserted] = map.insert( {"foo", bar} );
- Creates variables
it
andinserted
with deduced type from thepair
thatmap::insert
returns.
- Works with tuple/pair-likes &
std::array
s and relatively flat structs
- Basically, first-class
if (init; condition)
andswitch (init; condition)
if (const auto [it, inserted] = map.insert( {"foo", bar} ); inserted)
- Extends the
if(decl)
to cases wheredecl
isn't convertible-to-bool sensibly
Generalizing range-based for loops
- Appears to be mostly support for sentinals, or end iterators that are not the same type as begin iterators, which helps with null-terminated loops and the like.
Misc
-
- Finally!
- Not in all cases, but distingushes syntax where you are "just creating something" that was called elision, from "genuine elision".
Fixed order-of-evaluation for (some) expressions
- 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.
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.-
- Test if a header file include would be an error
- makes migrating from experimental to std almost seamless
inherited constructors fixes to some corner cases.
Library additions:
Data types
-
- Almost-always non-empty last I checked?
- Tagged union type
- {awesome|useful}
-
- Maybe holds one of something
- Ridiculously useful
-
- Holds one of anything (that is copyable)
-
std::string
like reference-to-character-array or substring- Never take a
string const&
again. Also can make parsing a bajillion times faster.
Invoke stuff
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::is_callable
std::make_from_tuple
,std::apply
applied to object construction
File System TS v1
[class.directory_iterator]
and[class.recursive_directory_iterator]
As of this writing, minor oops: no simple and portable way to open a file found this way. Hopefully fixed shortly.
Threading
-
- Untimed, which can be more efficient if you don't need it.
atomic<T>
::is_always_lockfree
-
- Saves some
std::lock
pain when locking more than one mutex at a time.
- Saves some
-
- The linked paper from 2014, may be out of date
- Parallel versions of
std
algorithms, and related machinery
(parts of) Library Fundamentals TS v1 not covered above or below
[func.searchers]
and[alg.search]
- A searching algorithm and techniques
-
- Polymorphic allocator, like
std::function
for allocators - And some standard memory resources to go with it.
- Polymorphic allocator, like
std::sample
, sampling from a range?
Container Improvements
try_emplace
andinsert_or_assign
- gives better guarantees in some cases where spurious move/copy would be bad
Splicing for
map<>
,unordered_map<>
,set<>
, andunordered_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
- like
The
emplace
family of functions now returns a reference to the created object.
Smart pointer changes
unique_ptr<T[]>
fixes and otherunique_ptr
tweaks.weak_from_this
and some fixed to shared from this
Other std
datatype improvements:
{}
construction ofstd::tuple
and other improvements- TriviallyCopyable reference_wrapper, can be performance boost
Misc
C++17 library is based on C11 instead of C99
Reserved
std[0-9]+
for future standard libraries-
- utility code already in most
std
implementations exposed
- utility code already in most
- Special math functions
- scientists may like them
std::clamp()
std::clamp( a, b, c ) == std::max( b, std::min( a, c ) )
roughly
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
- like
std::chrono
polishstd::conjunction
,std::disjunction
,std::negation
exposedstd::not_fn
- 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
.
Isocpp.org has a more comprehensive list of papers, some of which I pillaged for above. At this point it isn't up to date on what is accepted into C++17.
Naturally TS work continues in parallel, so there are some TS that are not-quite-ripe that will have to wait for the next iteration. The target for the next iteration is C++20 as previously planned, not C++19 as some rumors implied. C++1Ω has been avoided.
Initial list taken from this reddit post and this reddit post, with links added via googling or from the above isocpp.org page.
Additional entries pillaged from SD-6 feature-test list.
clang's feature list and library feature list are next to be pillaged. This doesn't seem to be reliable, as it is C++1z, not C++17.
these slides had some features missing elsewhere.
While "what was removed" was not asked, here is a short list of a few things ((mostly?) previous deprecated) that are removed in C++17 from C++:
Removed:
register
, keyword reserved for future usebool b; ++b;
- trigraphs
- if you still need them, they are now part of your source file encoding, not part of language
- ios aliases
- auto_ptr, old
<functional>
stuff,random_shuffle
- allocators in
std::function
There were rewordings. I am unsure if these have any impact on code, or if they are just cleanups in the standard:
Spec changes:
Further reference:
这篇关于C ++ 17的新特性是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!