简体   繁体   English

C++17 有哪些新特性?

[英]What are the new features in C++17?

C++17 is now feature complete, so unlikely to experience large changes. C++17 现在功能完整,所以不太可能经历大的变化。 Hundreds of proposals were put forward for 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 时,哪些特性将可用?

Language features:语言特点:

Templates and Generic Code模板和通用代码

Lambda拉姆达

Attributes属性

Syntax cleanup语法清理

Cleaner multi-return and flow control更清洁的多回程和流量控制

  • Structured bindings 结构化绑定

    • Basically, first-class std::tie with auto基本上,一流的std::tieauto
    • Example:示例:
      • const auto [it, inserted] = map.insert( {"foo", bar} );
      • Creates variables it and inserted with deduced type from the pair that map::insert returns.创建的变量itinserted从与推定的类型pair那个map::insert回报。
    • Works with tuple/pair-likes & std::array s and relatively flat structs适用于 tuple/pair-likes & std::array和相对扁平的结构
    • Actually named structured bindings in standard标准中实际命名的结构化绑定
  • if (init; condition) and switch (init; condition) if (init; condition)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.if(decl)扩展到decl不能明智地转换为 bool 的情况。
  • Generalizing range-based for loops 泛化基于范围的 for 循环

    • 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 如果 constexpr

    • Much requested feature to simplify almost-generic code.很多需要的功能来简化几乎通用的代码。

Misc杂项

Library additions:图书馆补充:

Data types数据类型

  • 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 std::string类似于对字符数组或子std::string引用
    • Never take a string const& again.永远不要再使用string const&了。 Also can make parsing a bajillion times faster.还可以使解析速度快 bajillion 倍。
    • "hello world"sv
    • constexpr char_traits constexpr char_traits
  • std::byte off more than they could chew. std::byte比他们可以咀嚼的还要多。

    • Neither an integer nor a character, just data既不是整数也不是字符,只是数据

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添加用于线程目的,即使您不使用线程也会暴露

Threading穿线

(parts of) Library Fundamentals TS v1 not covered above or below上面或下面未涵盖的(部分) 库基础知识 TS v1

Container Improvements容器改进

Smart pointer changes智能指针变化

Other std datatype improvements:其他std数据类型改进:

Misc杂项

Traits性状

Deprecated已弃用

Isocpp.org has has an independent list of changes since C++14; Isocpp.org 拥有自 C++14 以来的独立变更列表; it has been partly pillaged.它已被部分掠夺。

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.自然地,TS 工作并行继续,因此有些 TS 还不够成熟,必须等待下一次迭代。 The target for the next iteration is C++20 as previously planned, not C++19 as some rumors implied.下一次迭代的目标是之前计划的 C++20,而不是一些传言暗示的 C++19。 C++1O has been avoided.已避免使用 C++1O。

Initial list taken from this reddit post and this reddit post , with links added via googling or from the above isocpp.org page.这个 reddit 帖子这个 reddit 帖子中获取的初始列表,通过谷歌搜索或从上面的 isocpp.org 页面添加了链接。

Additional entries pillaged from SD-6 feature-test list.SD-6功能测试列表中掠夺的其他条目。

clang's feature list and library feature list are next to be pillaged. clang 的功能列表库功能列表即将被掠夺。 This doesn't seem to be reliable, as it is C++1z, not C++17.这似乎不可靠,因为它是 C++1z,而不是 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++:虽然没有询问“删除了什么”,但这里列出了在 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) P0505R0 (constexpr 计时)

  • P0418R2 (atomic tweaks) P0418R2 (原子调整)

  • P0512R0 (template argument deduction tweaks) P0512R0 (模板参数推导调整)

  • P0490R0 (structured binding tweaks) P0490R0 (结构化绑定调整)

  • P0513R0 (changes to std::hash ) P0513R0 (更改为std::hash

  • P0502R0 (parallel exceptions) P0502R0 (并行异常)

  • P0509R1 (updating restrictions on exception handling) P0509R1 (更新异常处理限制)

  • P0012R1 (make exception specifications be part of the type system) P0012R1 (使异常规范成为类型系统的一部分)

  • P0510R0 (restrictions on variants) P0510R0 (变体限制)

  • P0504R0 (tags for optional/variant/any) P0504R0 (可选/变体/任何标签)

  • P0497R0 (shared ptr tweaks) P0497R0 (共享 ptr 调整)

  • P0508R0 (structured bindings node handles) P0508R0 (结构化绑定节点句柄)

  • P0521R0 (shared pointer use count and unique changes?) P0521R0 (共享指针使用计数和唯一更改?)

Spec changes:规格变化:

Further reference:进一步参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM