C++17 有哪些新特性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38060436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:45:18  来源:igfitidea点击:

What are the new features in C++17?

c++standardsc++-faqc++17

提问by Yakk - Adam Nevraumont

C++17 is now feature complete, so unlikely to experience large changes. Hundreds of proposals were put forward for C++17.

C++17 现在功能完整,所以不太可能经历大的变化。为 C++17 提出了数百个提案。

Which of those features were added to C++ in C++17?

在 C++17 中,C++ 中添加了哪些特性?

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?

当使用支持“C++1z”的 C++ 编译器时,当编译器更新到 C++17 时,哪些特性将可用?

回答by Yakk - Adam Nevraumont

Language features:

语言特点:

Templates and Generic Code

模板和通用代码

Lambda

拉姆达

Attributes

属性

Syntax cleanup

语法清理

Cleaner multi-return and flow control

更清洁的多回程和流量控制

  • Structured bindings

    • Basically, first-class std::tiewith auto
    • Example:
      • const auto [it, inserted] = map.insert( {"foo", bar} );
      • Creates variables itand insertedwith deduced type from the pairthat map::insertreturns.
    • Works with tuple/pair-likes & std::arrays and relatively flat structs
    • Actually named structured bindingsin 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 declisn'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.
  • 结构化绑定

    • 基本上,一流std::tieauto
    • 例子:
      • const auto [it, inserted] = map.insert( {"foo", bar} );
      • 创建变量itinserted从与推导的类型pairmap::insert回报。
    • 适用于 tuple/pair-likes & std::arrays 和相对扁平的结构
    • 标准中实际命名的结构化绑定
  • if (init; condition)switch (init; condition)

    • if (const auto [it, inserted] = map.insert( {"foo", bar} ); inserted)
    • 扩展if(decl)decl不能转换为布尔值的情况。
  • 泛化基于范围的 for 循环

    • 似乎主要支持哨兵,或者与开始迭代器类型不同的结束迭代器,这有助于以空终止循环等。
  • 如果 constexpr

    • 许多要求的功能可以简化几乎通用的代码。

Misc

杂项

Library additions:

图书馆补充:

Data types

数据类型

Invoke stuff

调用东西

File System TS v1

文件系统 TS v1

New algorithms

新算法

  • for_each_n

  • reduce

  • transform_reduce

  • exclusive_scan

  • inclusive_scan

  • transform_exclusive_scan

  • transform_inclusive_scan

  • Added for threading purposes, exposed even if you aren't using them threaded

  • for_each_n

  • reduce

  • transform_reduce

  • exclusive_scan

  • inclusive_scan

  • transform_exclusive_scan

  • transform_inclusive_scan

  • 添加用于线程目的,即使您不使用线程也会暴露

Threading

穿线

(parts of) Library Fundamentals TS v1not covered above or below

上面或下面未涵盖的(部分)库基础知识 TS v1

Container Improvements

容器改进

Smart pointer changes

智能指针变化

Other stddatatype improvements:

其他std数据类型改进:

Misc

杂项

Traits

性状

Deprecated

已弃用

Isocpp.org hashas an independent list of changes since C++14; it has been partly pillaged.

Isocpp.org 拥有自 C++14 以来的独立变更列表;它已被部分掠夺。

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++1O has been avoided.

自然地,TS 工作并行继续,因此有些 TS 尚未成熟,将不得不等待下一次迭代。下一次迭代的目标是之前计划的 C++20,而不是一些传言暗示的 C++19。已避免使用 C++1O。

Initial list taken from this reddit postand this reddit post, with links added via googling or from the above isocpp.org page.

这个 reddit 帖子这个 reddit 帖子中获取的初始列表,通过谷歌搜索或从上面的 isocpp.org 页面添加了链接。

Additional entries pillaged from SD-6feature-test list.

SD-6功能测试列表中掠夺的其他条目。

clang's feature listand library feature listare next to be pillaged. This doesn't seem to be reliable, as it is C++1z, not C++17.

clang 的功能列表库功能列表即将被掠夺。这似乎不可靠,因为它是 C++1z,而不是 C++17。

these slideshad 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++:

虽然没有询问“删除了什么”,但这里列出了在 C++17 中从 C++ 中删除的一些内容((大部分?)以前已弃用):

Removed:

删除:

There were rewordings. I am unsure if these have any impact on code, or if they are just cleanups in the standard:

有改写。我不确定这些是否对代码有任何影响,或者它们是否只是标准中的清理:

Papers not yet integrated into above:

尚未整合到上面的论文:

  • 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?)

  • P0505R0(constexpr 计时)

  • P0418R2(原子调整)

  • P0512R0(模板参数推导调整)

  • P0490R0(结构化绑定调整)

  • P0513R0(更改为std::hash

  • P0502R0(并行异常)

  • P0509R1(更新异常处理限制)

  • P0012R1(使异常规范成为类型系统的一部分)

  • P0510R0(变体限制)

  • P0504R0(可选/变体/任何标签)

  • P0497R0(共享 ptr 调整)

  • P0508R0(结构化绑定节点句柄)

  • P0521R0(共享指针使用计数和唯一更改?)

Spec changes:

规格变化:

Further reference:

进一步参考: