C++17 新特性
# C++17 新特性
# 1、C++17特性概述
# 1.1 以下功能已合并到 C++17 中:
- 来自文件系统 TS (opens new window):文件系统库 (opens new window)
- 来自库基础 v1 TS (opens new window):std::any (opens new window)、std::optional (opens new window)、std::string_view (opens new window)、std::apply (opens new window)、多态分配器 (opens new window)、搜索器 (opens new window)等等
- 来自库基础 v2 TS (opens new window):std::void_t (opens new window)、std::conjunction (opens new window)、std::disjunction (opens new window)、std::negation (opens new window)、std::not_fn (opens new window)、std::gcd (opens new window)、std::lcm (opens new window)
- 来自并行 v1 TS (opens new window):执行策略 (opens new window)、std::reduce (opens new window)、std::inclusive_scan (opens new window)、std::exclusive_scan (opens new window) 等等,但不包括
exception_list
- 来自数学特殊函数 IS (opens new window):数学特殊函数 (opens new window)
- 来自 C11 (opens new window):std::aligned_alloc (opens new window)、std::timespec_get (opens new window)
# 1.2 以下功能在 C++17 中已经过时
移除:
- std::auto_ptr (opens new window)
- 已弃用的函数对象 (opens new window)
- std::random_shuffle (opens new window)
- std::unexpected (opens new window)
- 过时的
iostream
别名 (opens new window) - 三标符 (opens new window)
register
(opens new window) 关键字bool
类型的自增操作 (opens new window)- 动态异常说明 (opens new window)
弃用:
- std::iterator (opens new window)
- std::raw_storage_iterator (opens new window)
- std::get_temporary_buffer (opens new window)
- std::is_literal_type (opens new window)
- std::result_of (opens new window)
- 整个
<codecvt>
(opens new window) 头文件
# 1.3 新的语言特性
将
noexcept
(opens new window) 作为类型系统的一部分变量
模板
- 折叠表达式 (opens new window) ( ... )
- 类模板实参推导 (opens new window) tuple t(4, 3, 2.5)
auto
(opens new window) 占位的非类型模板形参 (opens new window)
-
- 简化的嵌套命名空间
using
声明语句可以声明多个名称- 属性命名空间 (opens new window)不必重复
-
[[fallthrough]]
[[maybe_unused]]
[[nodiscard]]
# 1.4 新的头文件
<any>
(opens new window)<charconv>
(opens new window)<execution>
(opens new window)<filesystem>
(opens new window)<memory_resource
(opens new window)<optional>
(opens new window)string_view
(opens new window)variant
(opens new window)
# 1.5 新的库特性
# 1.5.1 工具类型
- std::tuple (opens new window)
- std::any (opens new window)
- std::optional (opens new window)
- std::variant (opens new window)
- 搜索器 (opens new window)
- std::as_const (opens new window)
- std::not_fn (opens new window)
# 1.5.2 内存管理
- 未初始化内存算法
weak_from_this
(opens new window)- std::pmr::memory_resource (opens new window) 与 std::pmr::polymorphic_allocator (opens new window)
- std::aligned_alloc (opens new window)
- 通透的 std::owner_less (opens new window)
- std::shared_ptr (opens new window) 的数组支持
- 带有显式对齐的分配函数 (opens new window)
# 1.5.3 编译时编程
std::byte
(opens new window)- std::conjunction (opens new window)/std::disjunction (opens new window)/std::negation (opens new window)
- 类型特征 (opens new window)变量模板(
xxx_v
,如 std::is_same_v (opens new window)) - std::is_swappable (opens new window)
is_invocable
(opens new window)is_aggregate
(opens new window)- std::has_unique_object_representations (opens new window)
# 1.5.4 算法
- std::clamp (opens new window)
- 并行算法与执行策略 (opens new window)
- std::inclusive_scan (opens new window)
- std::exclusive_scan (opens new window)
- std::gcd (opens new window)
- std::lcm (opens new window)
- std::reduce (opens new window)
# 1.5.5 迭代器与容器
- map/set
extract
(opens new window) 与 map/setmerge
(opens new window) - map/unordered_map
try_emplace
(opens new window) 与insert_or_assign
(opens new window) - 连续迭代器(老式连续迭代器 (LegacyContiguousIterator) (opens new window))
- 非成员 std::size (opens new window)/std::empty (opens new window)/std::data (opens new window)
# 1.5.6 数值
# 1.5.7 其他
- 缓存线接口 (opens new window)
- std::launder (opens new window)
- std::uncaught_exceptions
std::to_chars
(opens new window)/std::from_chars
(opens new window)std::atomic::is_always_lock_free
(opens new window)std::scoped_lock
(opens new window)- std::timespec_get (opens new window)
- std::chrono::duration (opens new window) 与 std::chrono::time_point (opens new window) 的取整函数
# 2、noexcept 说明符
指定函数是否抛出异常。
语法 noexcept (1) 与 noexcept(true)
相同noexcept(表达式) (2) 如果 表达式 求值为 true,那么声明函数不会抛出任何异常。后随 noexcept
的(只能是该形式的一部分(它不是初始化器的开始))。throw() (3) 与 noexcept(true)
相同(C++17 前的语义见动态异常说明 (opens new window))(C++17 中弃用) (C++20 中移除)
noexcept 说明不是函数类型的一部分,而且只能在声明函数、变量、函数类型的非静态数据成员、函数指针、函数引用或成员函数指针时,以及在以上这些声明中声明类型为函数指针或函数引用的形参或返回类型时,作为 lambda 声明符 或 顶层函数声明符 的一部分出现。它不能在 typedef 或 类型别名 声明中出现。
C++17 之前
void f() noexcept; // 函数 f() 不会抛出
void (*fp)() noexcept(false); // fp 指向可能会抛出的函数
void g(void pfa() noexcept); // g 接收指向不会抛出的函数的指针
// typedef int (*fp)() noexcept; // 错误
C++17 起
noexcept 说明是函数类型的一部分,可以作为任何函数声明符的一部分出现。
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
上次更新: 2024/6/3 14:54:44