C++23
From cppreference.net
<
cpp
C++标准的当前修订版本。
|
本节内容尚不完整
原因:请确认所有重要特性均已提及(可参考下方编译器支持表格)。需补充更多链接并考虑重新归类部分条目。 |
目录 |
新语言特性
- 新增 语言特性测试宏
- 显式对象参数 、 显式对象成员函数 (亦称“推导 this ”)( P0847R7 )
- 多维下标运算符 (例如 v [ 1 , 3 , 7 ] = 42 ; )( P2128R6 )
- static operator [ ] ( P2589R1 )、 static operator ( ) 与静态 lambda( P1169R4 )
- auto ( x ) 与 auto { x } :语言中的 退化拷贝 ( P0849R8 )
-
通过新属性
[[ assume ( expression )]]进行假设( P1774R8 ) - Lambda 表达式 上的属性( P2173R1 )
- 可选的 扩展浮点类型 : std :: float { 16 | 32 | 64 | 128 } _t 与 std:: bfloat16_t ( P1467R9 )
-
新增预处理器指令:
#elifdef、#elifndef( P2334R1 )与 #warning ( P2437R1 ) -
(
signed
)
std::
size_t
字面量的
字面量后缀 '
Z'/'z' ( P0330R8 ),例如 auto ouz = 0uz ; - 行拼接前的空白符修剪( P2223R2 )
- 简化的隐式移动( P2266R3 )
- 基于范围的 for 循环初始化器中临时对象生命期延长( P2718R0 )
- 继承构造函数的 CTAD( P2582R1 )
-
复合语句末尾的标签(
P2324R2
),例如:
-
void
f
(
int
&
x
)
{
if ( x )
goto END ;
x = 42 ;
END :
}
-
初始化语句中的别名声明(
using
),例如
-
for
(
using
T
=
int
;
T e
:
v
)
/* ... */
-
使 lambda 表达式的
()更加可选( P1102R2 ) - 在 static_assert 和 if constexpr 中缩小上下文转换到 bool ( P1401R5 )
- 强制声明顺序布局(非静态类数据成员)( P1847R4 )
- 文本编码变更:
- if consteval / if not consteval ( P1938R3 )
- constexpr 变更:
新库特性
- 新增 库功能测试宏
新增模块
新增头文件
C 兼容性头文件:
- <stdatomic.h> ( P0943R6 )
库功能特性
通用工具集
- std:: expected :用于返回函数结果的新词汇类型( <expected> )( P0323R12 , P2549R1 )
- std:: move_only_function :仅移动的可调用包装器( P0288R9 )
- std:: bind_back :调用包装器( P2387R3 )
- std:: byteswap :反转字节序( P1272R4 )
- std:: forward_like ( P2445R1 )
- std:: invoke_r :调用 可调用 对象( P2136R3 )
- std:: to_underlying :获取 枚举 底层值的工具函数( P1682R3 )
- std:: unreachable :标记不可达代码的函数( P0627R6 )
-
std::
optional
(
P0798R8
)和
std::
expected
(
P2505R5
)的单子操作(
transform、or_else和and_then) - std:: tuple 兼容其他元组类对象( P2165R4 )
- 为 std:: pair 的转发构造函数添加默认参数( P2718R0 )
- std:: basic_common_reference 对 std:: reference_wrapper 的特化可生成引用类型( P2655R3 )
协程库
- std:: generator : 用于范围的同步 协程 生成器 ( P2502R2 , P2787R0 )
诊断
算法、迭代器与范围
-
新范围适配器:
- views:: adjacent 和 views:: adjacent_transform ( P2321R2 )
- views:: as_const ( P2278R4 )
- views:: as_rvalue ( P2446R2 )
- views:: cartesian_product ( P2374R4 )
- views:: chunk_by ( P2443R1 )
- views:: chunk ( P2442R1 )
- views :: enumerate ( P2164R9 )
- views:: join_with ( P2441R2 )
- views:: repeat ( P2474R2 )
- views:: slide ( P2442R1 )
- views:: stride ( P1899R3 )
- views:: zip , views:: zip_transform ( P2321R2 )
- ranges:: range_adaptor_closure :用于程序定义范围适配器闭包的辅助工具 ( P2387R3 )
- ranges:: to :范围转换函数 ( P1206R7 )
-
新约束范围算法:
- ranges:: starts_with 和 ranges:: ends_with ( P1659R3 )
- ranges:: contains 和 ranges:: contains_subrange ( P2302R4 )
- ranges:: find_last , ranges:: find_last_if , 和 ranges:: find_last_if_not ( P1223R5 )
- ranges:: iota , ranges:: shift_left , 和 ranges:: shift_right :非约束算法的范围化版本 ( P2440R1 )
- ranges:: fold_left 等: 范围折叠算法 ( P2322R6 )
- 修正常量迭代器、哨兵和范围,即 ranges:: cbegin 及其他类似工具应完全保证返回常量迭代器,即使对于浅层 const 视图(如 std:: span )( P2278R4 )
- 使某些视图的多参数构造函数显式化 ( P2711R1 )
- 范围迭代器作为非范围算法的输入 ( P2408R5 )
- 放宽范围适配器以允许移动仅类型 ( P2494R2 )
容器
- std:: mdspan :非拥有型多维数组引用( P0009R18 、 P2599R2 、 P2604R0 、 P2613R1 、 P2763R1 )
- 容器从其他兼容范围的可构造性与可赋值性( P1206R7 )
- std:: flat_set 、 std:: flat_multiset ( P1222R4 )、 std:: flat_map 、 std:: flat_multimap ( P0429R9 ):扁平集合与扁平映射——包装底层有序随机访问容器的容器适配器
- 允许在 std:: stack 和 std:: queue 中使用迭代器对进行构造( P1425R4 )
- 关联容器的异构擦除重载( P2077R2 )
- 容器推导指南中分配器的非推导上下文( P1518R2 )
- 要求 std:: span 和 std:: basic_string_view 可平凡复制( P2251R1 )
编译期支持
-
constexpr
支持:
- std:: bitset ( P2417R2 )
- std:: unique_ptr ( P2273R3 )
- std::type_info::operator== ( P1328R1 )
- 部分 <cmath> 函数 ( P0533R9 )
- std:: to_chars 和 std:: from_chars 的整型重载 ( P2291R3 )
-
元编程
工具:
- 为比较概念添加仅移动类型支持 ( P2404R3 )
- 类型特征:
内存管理
- std:: out_ptr 和 std:: inout_ptr :用于 C 语言互操作的智能指针适配器 ( P1132R7 )
- std :: allocate_at_least 和 std :: allocator :: allocate_at_least ( P0401R6 )
- std:: start_lifetime_as :用于隐式生命周期类型的显式生命周期管理函数 ( P2590R2 )
- 禁止用户对 std:: allocator_traits 进行特化 ( P2652R2 )
字符串与文本处理
-
字符串类型的新成员函数及变更:
- std :: basic_string :: contains 与 std :: basic_string_view :: contains ( P1679R3 )
- 禁用从 nullptr 构造 std:: basic_string 和 std:: basic_string_view ( P2166R1 )
- std:: basic_string_view 的显式范围构造函数 ( P1989R2 )
- std :: basic_string :: resize_and_overwrite ( P1072R10 )
- std :: basic_string :: substr 的右值引用重载,用于高效切片 ( P2438R2 )
- 格式化范围、元组、字符与字符串的转义表示、 std:: thread :: id 以及 堆栈跟踪 ( P2286R8 ) ( P2585R1 ) ( P2693R1 )
输入/输出与打印
- 头文件 <print> 中提供的格式化输出函数 std:: print 和 std:: println ( P2093R14 )
-
头文件
<spanstream>
中提供的
spanstream库(基于 std:: span 的字符串流)( P0448R4 ) - 在 std :: fstreams 中支持独占模式 ( P2467R1 )
- 支持输出 volatile T * : std :: basic_ostream :: operator << ( const volatile void * ) ( P1147R1 )
已弃用功能
已移除功能
- 垃圾回收支持与基于可达性的泄漏检测。( P2186R2 )
- 支持混合宽字符串字面量 拼接 ,例如 const auto * no = u "q" U "p" 属于非法格式。( P2201R1 )
- 不可编码的宽字符字面量与多字符宽字符字面量,例如 wchar_t x = 'db' 属于非法格式。( P2362R3 )
已弃用功能
- std:: aligned_storage 和 std:: aligned_union ( P1413R3 )
- std:: numeric_limits :: has_denorm ( P2614R2 )
已撤销的弃用项
- 下标表达式中的 operator, (逗号运算符)(但其语义已更改为支持可重载的多维下标 operator [ ] )。
-
部分C语言头文件(为兼容C语言保留的对应
<*.h>头文件)( P2340R1 )
缺陷报告
编译器支持
主要文章: C++23 compiler support
C++23 核心语言特性
|
C++23 特性
|
提案文档
|
GCC
|
Clang
|
MSVC
|
Apple Clang
|
EDG eccp
|
Intel C++
|
Nvidia HPC C++ (ex PGI)*
|
Nvidia nvcc
|
Cray
|
|
|---|---|---|---|---|---|---|---|---|---|---|---|
| 字面量后缀 用于 (有符号) size_t ( FTM ) * | P0330R8 | 11 | 13 | 19.43* | 13.1.6* | 6.5 | 2022.2 | 23.9 | |||
使
()
对
lambda 表达式
更可选
|
P1102R2 | 11 | 13 | 19.44* | 13.1.6* | 6.3 | 2022.2 | 22.5 | |||
| if consteval ( FTM ) * | P1938R3 | 12 | 14 | 19.44* | 14.0.0* | 6.3 | 2022.2 | 24.1 | |||
| 移除垃圾回收支持 | P2186R2 | 12 | 不适用 | 19.30* | 不适用 | 不适用 | |||||
| static_assert 和 constexpr if 中的窄化上下文转换 | P1401R5 | 9 |
13 (部分)*
14 |
14.0.0* | 2022.2 | 20.7 | |||||
| 行拼接前修剪空白字符 | P2223R2 | 是 | 是 | 是 | 6.7 | 2022.2 | 是 | ||||
| 使声明顺序布局成为强制要求 | P1847R4 | 是 | 是 | 是 | 是 | 2022.2 | 是 | ||||
| 移除混合宽字符串字面量 拼接 | P2201R1 | 是 | 是 | 是 | 是 | 是 | 是 | 是 | |||
| 显式对象成员函数 (推导 this ) ( FTM ) * | P0847R7 | 14 |
18
19* |
19. |
C++23 标准库特性
|
C++23 feature
|
Paper(s)
|
GCC libstdc++
|
Clang libc++
|
MSVC STL
|
Apple Clang*
|
|
|---|---|---|---|---|---|---|
| Stacktrace library ( FTM ) * |
P0881R7
P2301R1 |
12 (partial)*
14* |
19.34* | |||
| <stdatomic.h> ( FTM ) * | P0943R6 | 12 | 15 | 19.31* | 14.0.3* | |
| std::is_scoped_enum ( FTM ) * | P1048R1 | 11 | 12 | 19.30* | 13.0.0* | |
| std::basic_string::contains() , std::basic_string_view::contains() ( FTM ) * | P1679R3 | 11 | 12 | 19.30* | 13.0.0* | |
| std::to_underlying ( FTM ) * | P1682R3 | 11 | 13 | 19.30* | 13.1.6* | |
| Relaxing requirements for time_point<>::clock | P2212R2 | N/A | N/A | N/A | N/A | |
| Providing size feedback in the Allocator interface ( FTM ) * | P0401R6 | 15 | 19.30* | 14.0.3* | ||
| <spanstream> : string-stream with std::span -based buffer ( FTM ) * | P0448R4 | 12 | 19.31* | |||
| std::out_ptr() , std::inout_ptr() ( FTM ) * | P1132R8 | 14 | 19 | 19.30* | ||
| constexpr type_info::operator==() ( FTM ) * | P1328R1 | 12 | 17 |
19.33*
*
19.34* |
15.0.0* | |
| Iterator pair constructors for std::stack and std::queue ( FTM ) * | P1425R4 | 12 | 14 | 19.31* | 14.0.3* | |
| Non-deduction context for allocators in container deduction guides | P1518R2 | 12 * | 13 * | 19.31* * | 13.1.6* * | |
| ranges::starts_with() and ranges::ends_with() ( FTM ) * | P1659R3 |
17 (partial)*
18 |
19.31* | 16.0.0* | ||
| Prohibiting std::basic_string and std::basic_string_view construction from nullptr | P2166R1 | 12 | 13 | 19.30* | 13.1.6* | |
| std::invoke_r() ( FTM ) * | P2136R3 | 12 | 17 | 19.31* | 15.0.0* | |
| Range constructor for std::basic_string_view | P1989R2 | 11 | 14 | 19.30* | 14.0.3* | |
| Default template arguments for std::pair 's forwarding constructor | P1951R1 | 14 | 14 | 19.30* | 14.0.3* | |
| Remove Garbage Collection and Reachability-Based Leak Detection ( library support ) | P2186R2 | 12 | 14 | 19.30* | 14.0.3* | |
| zip: views::zip , views::zip_transform , views::adjacent , and views::adjacent_transform ( FTM ) * | P2321R2 | 13 | 15 (partial)* |
19.33*
(partial)*
*
*
19.37* |
14.0.3* | |
| Heterogeneous erasure overloads for associative containers ( FTM ) * | P2077R3 | 19.32* | ||||
| std::byteswap() ( FTM ) * | P1272R4 | 12 | 14 | 19.31* | 14.0.3* | |
| Printing volatile T * | P1147R1 | 11.3 | 14 | 19.31* | 14.0.3* | |
| basic_string::resize_and_overwrite() ( FTM ) * | P1072R10 | 12 | 14 | 19.31* | 14.0.3* | |
| Monadic operations for std::optional ( FTM ) * | P0798R8 | 12 | 14 | 19.32* | 14.0.3* | |
| std::move_only_function ( FTM ) * | P0288R9 | 12 | 19.32* | |||
| Add a conditional noexcept specification to std::exchange | P2401R0 | 12 | 14 | 19.25* | 14.0.3* | |
| Require std::span & std::basic_string_view to be TriviallyCopyable | P2251R1 | Yes | Yes | Yes | Yes | |
| Clarifying the status of the “C headers” | P2340R1 | Yes | Yes | Yes | Yes | |
| <expected> ( FTM ) * |
P0323R12
P2549R1 |
12 | 16 | 19.33* | 15.0.0* | |
| constexpr for <cmath> and <cstdlib> ( FTM ) * | P0533R9 | 4.6 (partial)* | ||||
| std::unreachable() ( FTM ) * | P0627R6 | 12 | 15 | 19.32* | 14.0.3* | |
| Deprecating std::aligned_storage and std::aligned_union | P1413R3 | 13 | 16 | 19.33* | ||
| std::reference_constructs_from_temporary & std::reference_converts_from_temporary ( FTM ) * | P2255R2 |
13 (partial)*
14 |
21 (partial)* | |||
| constexpr std::unique_ptr ( FTM ) * | P2273R3 | 12 | 16 | 19.33* | 15.0.0* | |
| Constructing containers and strings from ranges with ranges::to() , tagged constructors, insert and assign member functions ( FTM ) * ( FTM ) * | P1206R7 |
14 (partial)*
15 |
17 | 19.34* | 15.0.0* | |
| Pipe support for user-defined range adaptors ( ranges::range_adaptor_closure , std::bind_back ) ( FTM ) * ( FTM ) * | P2387R3 |
13 (partial)*
14 |
19 | 19.34* | ||
| ranges::iota() , ranges::shift_left() , and ranges::shift_right() ( FTM ) * ( FTM ) * | P2440R1 | 13 (partial)* | 19.34* | |||
| views::join_with ( FTM ) * | P2441R2 | 13 | 19.34* | |||
| views::chunk and views::slide ( FTM ) * ( FTM ) * | P2442R1 | 13 | 19.33* | |||
| views::chunk_by ( FTM ) * | P2443R1 | 13 | 18 | 19.33* | 16.0.0* | |
| std::mdspan : a non-owning multidimensional array reference ( FTM ) * |
P0009R18
P2599R2 P2604R0 P2613R1 P2763R1 |
17 (partial)*
18 |
19.39* | 15.0.0* | ||
| <flat_map> ( FTM ) * | P0429R9 | 15 | 20 | |||
| <flat_set> ( FTM ) * | P1222R4 | 15 | 21 | |||
| ranges::find_last() , ranges::find_last_if() , and ranges::find_last_if_not() ( FTM ) * | P1223R5 | 13 | 19 | 19.36* | ||
| Freestanding Library: Easy <utility> , <ranges> , and <iterator> | P1642R11 | 13 * | N/A | |||
| views::stride ( FTM ) * | P1899R3 | 13 | 19.34* | |||
| Compatibility between std::tuple and tuple-like objects ( FTM ) * | P2165R4 | 14 | 2.9 (partial)* |
19.36*
(partial)
*
19.37* |
partial* | |
| Rectifying constant iterators, sentinels, and ranges. views:: as_const ( FTM ) * | P2278R4 | 13 |
19.35*
(partial)*
19.36* |
|||
| Formatting Ranges ( FTM ) * | P2286R8 |
15 (partial)*
15.2 |
16 |
19.37*
(partial)*
19.39* (partial)* 19.41* (partial)* 19.42* |
15.0.0* | |
| Improve default container formatting ( FTM ) * | P2585R1 | 15 |
16 (partial)*
17 |
19.42* | 15.0.0* | |
| Formatted output library <print> ( FTM ) * |
P2093R14
P2539R4 |
14 |
17 (partial)*
18 |
19.37* |
15.0.0*
*
16.0.0* |
|
| Formatting std::thread::id and std::stacktrace ( FTM ) * | P2693R1 | 14 | 17 (partial)* | 19.38* | 15.0.0* | |
| constexpr for integral overloads of std::to_chars() and std::from_chars() ( FTM ) * | P2291R3 | 13 | 16 | 19.34* | 15.0.0* | |
| ranges::contains() and ranges::contains_subrange() ( FTM ) * | P2302R4 | 13 |
18 (partial)*
19 |
19.34* | 16.0.0* * | |
| Ranges fold algorithms ( FTM ) * | P2322R6 | 13 | 19.35* | |||
| views::cartesian_product ( FTM ) * |
P2374R4
P2540R1 |
13 | 19.37* | |||
Adding move-only types support for comparison concepts (
equality_comparable
,
totally_ordered
,
three_way_comparable
) (
FTM
)
*
|
P2404R3 | 19.36* | ||||
| Ranges iterators as inputs to non-ranges algorithms ( FTM ) * | P2408R5 | 19.34* | ||||
| constexpr std::bitset ( FTM ) * | P2417R2 | 13 | 16 | 19.34* | 15.0.0* | |
basic_string::substr()
&&
|
P2438R2 | 16 | 19.34* | 15.0.0* | ||
| views::as_rvalue ( FTM ) * | P2446R2 | 13 | 16 | 19.34* | 15.0.0* | |
| Standard Library Modules ( FTM ) * | P2465R3 | 15* | 17 (partial)* |
19.35*
(partial)* 19.36* |
||
| std::forward_like() ( FTM ) * | P2445R1 | 14 | 16 | 19.34* | 15.0.0* | |
| Support exclusive mode for std::fstream ( FTM ) * | P2467R1 | 12 | 18 | 19.36* | 16.0.0* | |
| views::repeat ( FTM ) * | P2474R2 | 13 | 17 | 19.36* | 15.0.0* | |
| Relaxing range adaptors to allow for move-only types ( FTM ) * | P2494R2 | 14 | 17 | 19.34* | 15.0.0* | |
| std::basic_string_view range constructor should be explicit | P2499R0 | 12.2 | 16 | 19.34* | 15.0.0* | |
| std::generator : synchronous coroutine generator for ranges ( FTM ) * |
P2502R2
P2787R0 |
14 | 19.43* | |||
| Add a conditional noexcept specification to std::apply | P2517R1 | 10 | 18 | 19.34* | 16.0.0* | |
| Explicit lifetime management ( std::start_lifetime_as ) ( FTM ) * |
P2590R2
P2679R2 |
|||||
| Clarify handling of encodings in localized formatting of chrono types ( FTM ) * | P2419R2 | 15* | 19.34* * | |||
std::move_iterator
should not always be
input_iterator
(
FTM
)
*
|
P2520R0 | 12.3* | 17* | 19.34* * | 15.0.0* | |
| Deduction guides update for explicit object parameter call operators | LWG3617 | 14 | 19.34* | |||
| Deduction guides update for static operator ( ) | P1169R4 | 13 | 16 | 19.39* | 15.0.0* | |
| Standard names and library support for extended floating-point types | P1467R9 | 13 | 19.37* * | |||
| Monadic operations for std::expected ( FTM ) * | P2505R5 | 13 | 17 | 19.36* | 15.0.0* | |
| views::enumerate ( FTM ) * | P2164R9 | 13 | 19.37* | |||
| std::is_implicit_lifetime ( FTM ) * | P2674R1 | 20 | ||||
| std::common_reference_t of std::reference_wrapper should be a reference type ( FTM ) * ( FTM ) * | P2655R3 | 19.37* | ||||
| Disallowing user specialization of std::allocator_traits | P2652R2 | 19 | 19.37* | |||
| Deprecating std::numeric_limits::has_denorm | P2614R2 | 18 | 19.37* | 16.0.0* | ||
| Making Multi-Param Constructors Of view s explicit | P2711R1 | 17 (partial)* | 19.36* * | 15.0.0* | ||
| DR20: Relaxing Ranges Just A Smidge ( FTM ) * | P2609R3 | 15 | 20 | 19.37* | ||
| DR20: Stashing Stashing Iterators For Proper Flattening | P2770R0 | 14 | 18 | 19.37* | ||
| DR17: std::visit() for classes derived from std::variant ( FTM ) * | P2162R2 | 11.3 | 13 |
19.20*
*
19.30* |
13.1.6* | |
| DR20: Conditionally borrowed ranges | P2017R1 | 11 | 16 | 19.30* | ||
| DR20: Repairing input range adaptors and std::counted_iterator | P2259R1 | 12 |
19.30*
(partial)*
19.31* |
|||
| DR20: views::join should join all views of ranges | P2328R1 | 11.2 | 15 | 19.30* | 14.0.3* | |
DR20:
view
does not require
default_initializable
(
FTM
)
*
|
P2325R3 | 11.3 | 16 | 19.30* | 15.0.0* | |
| DR20: Range adaptor objects bind arguments by value | P2281R1 | 11 | 14 |
19.29 (16.10)*
(partial)* 19.31* |
14.0.3* | |
| DR20: constexpr for std::optional and std::variant ( FTM ) * ( FTM ) * | P2231R1 |
11.3 (partial)*
12 |
13 (partial)*
19 |
19.31* | 13.1.6* (partial) | |
| DR20: views::lazy_split and redesigned views::split | P2210R2 | 12 | 16 | 19.31* | 15.0.0* | |
| DR20: Fix ranges::istream_view | P2432R1 | 12 | 16 | 19.31* | 15.0.0* | |
DR20:
view
with ownership (
FTM
)
*
|
P2415R2 | 12 | 14 | 19.31* | 14.0.3* | |
| DR20: Fixing locale handling in chrono formatters ( FTM ) * |
P2372R3
P2418R2 |
13 | 19.31* | |||
| DR20: Cleaning up integer-class types | P2393R1 | 19.32* | ||||
| DR20: Compile-time format string checks; Reducing binary code size of std::format() family ( FTM ) * | P2216R3 | 13 |
14 (partial)*
15 |
19.32* | 14.0.3* | |
| DR20: Add support for non-const-formattable types to std::format | P2418R2 | 13 | 15 | 19.32* | 14.0.3* | |
| DR20: std::basic_format_string | P2508R1 | 13 | 15 | 19.35* | 14.0.3* | |
| DR20: Poison Pills are Too Toxic ( FTM ) * | P2602R2 | 14 | 19 | 19.36* | ||
| DR20: std::format fill character allowances | P2572R1 | 14 | 17 | 19.37* | ||
| DR20: Improving std::format 's width estimation | P2675R1 | 14 | 17 | 19.38* | ||
| DR20: std::barrier 's phase completion guarantees ( FTM ) * | P2588R3 |
19.28 (16.8)*
*
19.36* |
||||
|
C++23 feature |
Paper(s) |
GCC libstdc++
|
Clang libc++
|
MSVC STL
|
Apple Clang*
|
*
- 将鼠标悬停在标有星号
*
的单元格上可查看附加弹窗注释。
DR
nn
- "DR" 后的数字
nn
表示缺陷报告所适用的目标 C++ 修订版本,例如:DR20 → C++20。
外部链接
| 1. | C++23 - 维基百科 |