C ++ 17的新特性是什么?

编程入门 行业动态 更新时间:2024-10-18 16:50:40
本文介绍了C ++ 17的新特性是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

C ++ 17现在功能齐全,因此不太可能遇到大的更改。已经为C ++ 17提出了数百个提案。

哪些功能在C ++ 17中添加到C ++?

当使用支持C ++ 1z的C ++编译器时,当编译器更新为C ++ 17时,哪些功能将可用?

解决方案

语言特性: 模板和通用代码

  • 类模板的模板参数扣除

    • 就像函数如何推导模板参数一样,现在构造函数可以推导出类模板参数
  • 模板< auto>

    • 表示任何(非类型模板参数)类型的值
  • 非类型模板参数修复

  • 模板< template< class ...> typename bob> struct foo {}

  • (折叠+ ... +表达式)

  • auto x {8}; 是 int

  • a href =open-std/JTC1/SC22/WG21/docs/papers/2016/p0128r1.html =nofollow> constexpr if

Lambda
  • constexpr lambdas

    • Lambdas如果符合资格则默认为constexpr
  • 在lambdas中捕获 *此

    • [* this] {std :: cout<可以< be<<有用的< '\\\'; }
属性
  • [[fallthrough]] , [[nodiscard]] ,<$ c $ std/jtc1/sc22/wg21/docs/papers/2014/n4266.htmlrel =nofollow> [[属性]] 在

  • 编译器现在需要忽略他们无法识别的非标准属性。

    • C ++ 14的措辞允许编译器拒绝未知范围的属性。
    li>
语法cleanup
  • 内置变量

    • 与内联函数类似
    • 编译器选择实例实例化的位置?
    / li>
  • 命名空间A :: B

  • 简单 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' 字符文字。

  • __ has_include

    • 测试头文件是否包含错误
    • 几乎从实验迁移到std无缝
  • 指针转换修正数组

  • 继承了构造函数修复了一些角点。

库添加: 数据类型 b $ b
  • std :: variant< Ts ...>

      $ b $ b $ b b $ b
  • std :: optional

    • 可能持有某物
    • 太可用了
  • a href =www.open-std/jtc1/sc22/wg21/docs/papers/2015/n4480.html#any =nofollow> std :: any

    • 持有其中任何一项(可复制)
  • std :: string_view

    • 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 =www.open-std/jtc1/sc22/wg21/docs/papers/2016/p0218r0.html#class-path =nofollow> [class .path]

  • [class.filesystem.error]

  • [class.file_status]

  • [class。 directory_entry]

  • [class.directory_iterator] 和 [class.recursive_directory_iterator]

  • [fs.ops.funcs]

  • 在撰写本文时,小写:没有简单和可移植的方式来打开一个文件。希望稍后修复。

线程
  • std :: shared_mutex

    • Untimed,
    • =www.open-std/jtc1/sc22/wg21/docs/papers/2016/p0152r1.html =nofollow> :: is_always_lockfree

    • lock_guard< Mutexes ...>

      • 在每次锁定多个互斥体时节省一些 std :: lock 。
        • Parallelism TS v1

          • 2014年的链接论文可能已过期
          • std 算法和相关机制的并行版本
        • 硬件_ * _ interference_size

        (部分)图书基础知识TS v1 以上或以下不包括
        • [func.searchers] 和 [alg.search]
          • 搜索算法和技巧
        • [pmr]

          • 多边形分配器,例如 std :: function >
          • 和一些标准内存资源。
        • std :: sample

        容器改进
        • try_emplace 和 insert_or_assign

            $ www.open-std/jtc1/sc22/wg21/docs/papers/2016/p0083r3.pdf =nofollow>拼接地图<> , unordered_map , set<> 和 unordered_set<

            • 在容器之间廉价移动节点。
          • non-const .data() 。 / p>

          • 非会员 std :: size , std :: empty , std :: data

            • like / code> / end
          • 容器中的最小不完全类型支持

          • 连续迭代器概念

          • constexpr 迭代器

          • emplace 家庭功能现在返回对创建对象的引用。

          智能指针变更
          • unique_ptr< T []> 修复和其他 unique_ptr tweaks。
          • weak_from_this 和一些固定为分享的
          其他 std 数据类型改进:
          • {} 构建 std :: tuple 和其他改进
          • TriviallyCopyable reference_wrapper ,可以提升效果
          Misc
          • C ++ 17库基于 C11而非C99

          • 为 std [0-9] + /2016/p0180r1.htmlrel =nofollow>未来标准库

          • destroy(_at | _n), uninitialized_move(_n), uninitialized_value_construct(_n), uninitialized_default_construct(_n)

            • 实用程式程式码已包含在大多数 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 一个更全面的文件列表,其中一些我为上面抢救。在这一点上,它是不是最新的接受到C ++ 17。

          自然TS工作并行继续,所以有一些TS不是 - - 成熟的,将不得不等待下一次迭代。下一次迭代的目标是以前计划的C ++ 20,而不是一些谣言暗示的C ++ 19。已避免C ++1Ω。

          初始列表取自此文章和此

          从 SD-6 功能测试列表。

          clang的功能列表和图书馆功能列表旁边是被抢。这似乎不可靠,因为它是C ++ 1z,而不是C + + 17。

          这些幻灯片在其他地方缺少某些功能。

          While 什么被删除没有问,这里是一个简单的几个东西((多数?)以前弃用),在C ++ 17从C ++中删除:

          已移除:
          • 注册 ,关键字保留以备将来使用
          • bool b; ++ b;
          • trigraphs
            • 如果您仍然需要他们,他们现在是您的一部分源文件编码,而不是语言的一部分
          • ios别名
          • auto_ptr,old < functional> stuff, random_shuffle
          • std :: function 中的分配器

          有改动。我不确定这些是否对代码有任何影响,或者如果他们只是在标准清理:

          规格更改:
          • 异常规范和抛出表达式
          • b
          • 按年份分组的论文;不是全部接受

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

          • auto x{8}; is an int

          • constexpr if

            • Much requested feature to simplify almost-generic code
          Lambda
          • constexpr lambdas

            • Lambdas are implicitly constexpr if they qualify
          • Capturing *this in lambdas

            • [*this]{ std::cout << could << " be " << useful << '\n'; }
          Attributes
          • [[fallthrough]], [[nodiscard]], [[maybe_unused]] attributes

          • [[attributes]] on namespaces and enum { erator[[s]] }

          • 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
          • Inline variables

            • Like inline functions
            • Compiler picks where the instance is instantiated?
          • namespace A::B

          • Simple static_assert(expression); with no string

          Cleaner multi-return and flow control
          • 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
          • 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 sentinals, or end iterators that are not the same type as begin iterators, which helps with null-terminated loops and the like.
          Misc
          • Hexadecimal float point literals

          • Dynamic memory allocation for over-aligned data

          • Guaranteed copy elision

            • 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.

          • "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.

          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
            • 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.path]

          • [class.filesystem.error]

          • [class.file_status]

          • [class.directory_entry]

          • [class.directory_iterator] and [class.recursive_directory_iterator]

          • [fs.ops.funcs]

          • As of this writing, minor oops: no simple and portable way to open a file found this way. Hopefully fixed shortly.

          Threading
          • std::shared_mutex

            • Untimed, which can be more efficient if you don't need it.
          • atomic<T>::is_always_lockfree

          • lock_guard<Mutexes...>

            • Saves some std::lock pain when locking more than one mutex at a time.
          • Parallelism TS v1

            • The linked paper from 2014, may be out of date
            • Parallel versions of std algorithms, and related machinery
          • hardware_*_interference_size

          (parts of) Library Fundamentals TS v1 not covered above or below
          • [func.searchers] and [alg.search]
            • A searching algorithm and techniques
          • [pmr]

            • Polymorphic allocator, like std::function for allocators
            • And some standard memory resources to go with it.
          • std::sample, sampling from a range?

          Container Improvements
          • 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.

          Smart pointer changes
          • unique_ptr<T[]> fixes and other unique_ptr tweaks.
          • weak_from_this and some fixed to shared from this
          Other std datatype improvements:
          • {} construction of std::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

          • 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
          • 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
          • 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 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 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 use
          • bool 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:
          • exception specs and throw expressions
          Further reference:
          • papers grouped by year; not all accepted

更多推荐

C ++ 17的新特性是什么?

本文发布于:2023-11-29 20:22:59,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:新特性

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!