C++23
外观
C++23,正式名称ISO/IEC 14882:2024[1],是继C++20之后的C++编程语言开放标准。2020年2月,在捷克布拉格举行的C++20最终会议上,通过了C++23的总体计划。[2][3]第一次针对C++23的WG21会议原定于2020年6月在保加利亚瓦尔纳举行,但由于2019冠状病毒病疫情而被迫取消。[4][5] 原定于2020年11月在美国纽约召开的会议也被取消。[6] 该标准于2023年2月由WG21在美国伊瑟阔举行的混合会议上从技术上最终确定。[7]
新的“Hello, world”示例
[编辑]C++23引入了一些新功能,使用新的标准库头文件<print>列印出Hello, world的程序示例如下:
import std;
int main() {
std::println("Hello, world!");
}
特性
[编辑]被接受纳入 C++23 的变更包括:
语言特性
[编辑]- 显式
this对象参数[8] if consteval[9]- 多维下标运算符[10]
- 静态调用运算符、静态下标运算符和静态 lambda[11][12]
- 简化隐式移动(implicit move)[13]
auto(x)与auto{x}初始化[14]- 新的预处理指令:
- 在基于范围的 for 循环中延长某些临时对象的生命周期[17]
- 新的标准属性
[[assume(expression)]][18] - 继承构造函数的类模板参数推导[19]
- 复合语句(compound statement)末尾的标签(label)[20]
- 初始化语句(init-statement)中允许别名声明(alias declaration)[21]
- 为
std::size_t及其对应有符号类型添加字面量后缀[22] - 扩展浮点类型及其字面量(有条件支持)[23]
- 允许可选省略零参数 lambda 表达式中的
()[24] - Lambda 表达式上的属性支持[25]
constexpr相关改动:- 将
static_assert和if constexpr中的上下文收窄转换(contextual conversion)收窄到bool[29] - 在行拼接(line splicing)之前修剪空白字符[30]
- 强制声明顺序决定内存布局[31]
- 分隔的转义序列(delimited escape sequences)[32]
- 命名的通用字符转义(named universal character escapes)[33]
- 文本编码相关变化:
标准库
[编辑]标准库模块支持
[编辑]协程库支持
[编辑]- 用于范围的同步协程
std::generator[38]
通用工具支持
[编辑]- 结果类型
std::expected[39] - 为
std::optional[40] 和std::expected[41] 添加单子操作 - 用于获取枚举底层值的工具函数
std::to_underlying[42] - 仅可移动的可调用包装器
std::move_only_function[43] std::forward_like[44]std::invoke_r[45]std::bind_back[46]std::byteswap[47]std::unreachable:标记不可达代码的函数[48]- 使
std::tuple与其他“类似元组”的对象兼容[49] - 为
std::reference_wrapper提供std::basic_common_reference特化以生成引用类型[50] - 为
std::pair的转发构造函数添加默认参数[51]
编译时支持
[编辑]- 对以下内容添加
constexpr支持: - 元编程工具:
- 为比较概念添加仅可移动类型支持[60]
迭代器、范围和算法支持
[编辑]范围转换工具std::ranges::to[61]
- 新的受约束范围算法:
- 新的
std::ranges::range_adaptor_closure,一个用于程序定义的范围适配器闭包的辅助类[46] - 新的范围适配器:
std::views::zip系列,包含zip_transformstd::views::adjacent系列,包含adjacent_transform[67]std::views::join_with[68]std::views::slidestd::views::chunk[69]std::views::chunk_by[70]std::views::as_rvalue[71]std::views::as_const[72]std::views::repeat[73]std::views::stride[74]std::views::cartesian_product[75]std::views::enumerate[76]
- 修补常量迭代器、哨位和范围,即
std::ranges::cbegin与其他类似的返回常量迭代器的工具,即使对浅 const 视图(如std::span)也应提供完全保证[72] - ranges iterators as inputs to non-ranges algorithms[77]
- 以范围迭代器作为非范围算法的输入[78]
- 放宽范围适配器以允许仅移动类型[79]
- 使一些视图的多参数构造函数为显式的 [80]
注释
[编辑]- ^ ISO/IEC 14882:2024 Programming languages — C++. iso.org. [2025-03-13]. (原始内容存档于2021-10-06).
- ^ 2020 Prague Meeting Invitation and Information (PDF). [2020-09-08]. (原始内容存档 (PDF)于2019-12-29).
- ^ Author, Not Given. To boldly suggest an overall plan for C++23. www.open-std.org. 1968-01-01 [2020-09-08]. (原始内容存档于2020-05-23).
- ^ P2145R0: Evolving C++ Remotely. www.open-std.org. [2020-09-08]. (原始内容存档于2021-03-01).
- ^ Business Plan and Convener's Report: ISO/IEC JTC1/SC22/WG21 (C++) (PDF). [2020-09-08]. (原始内容 (PDF)存档于2021-10-23).
- ^ Upcoming Meetings, Past Meetings : Standard C++. isocpp.org. [2020-09-08]. (原始内容存档于2020-09-07).
- ^ C++23 "Pandemic Edition" is complete (Trip report: Winter ISO C++ standards meeting, Issaquah, WA, USA). herbsutter.com. 13 February 2023 [2025-03-13]. (原始内容存档于2026-03-08).
- ^ Gašper Ažman; Sy Brand; Ben Deane; Barry Revzin. Deducing this. 2021-07-12 [2025-04-28]. (原始内容存档于2026-05-21).
- ^ Barry Revzin; Richard Smith; Andrew Sutton; Daveed Vandevoorde. if consteval. 2021-03-22 [2025-04-28]. (原始内容存档于2026-06-01).
- ^ Mark Hoemmen; Daisy Hollman; Corentin Jabot; Isabella Muerte; Christian Trott. Multidimensional subscript operator (PDF). 2021-09-14 [2025-04-28]. (原始内容存档 (PDF)于2026-05-20).
- ^ static operator(). 2022-04-08 [2025-04-28]. (原始内容存档于2022-07-29).
- ^ static operator[] (PDF). 2022-11-11 [2025-04-28]. (原始内容存档 (PDF)于2026-05-24).
- ^ Simpler implicit move. 2022-03-23 [2025-04-28]. (原始内容存档于2022-05-24).
- ^ Zhihao Yuan. auto(x): decay-copy in the language. 2021-07-12 [2025-04-28]. (原始内容存档于2026-06-11).
- ^ Melanie Blower. Add support for preprocessing directives elifdef and elifndef (PDF). 2021-04-30 [2025-04-28]. (原始内容存档 (PDF)于2026-05-19).
- ^ Aaron Ballman. Support for #warning (PDF). 2022-01-13 [2025-04-28]. (原始内容存档 (PDF)于2022-07-30).
- ^ Wording for P2644R1 Fix for Range-based for Loop. 2022-11-11 [2025-04-28]. (原始内容存档于2022-12-19).
- ^ Portable assumptions (PDF). 2022-04-22 [2025-04-28]. (原始内容存档 (PDF)于2022-11-06).
- ^ Timur Doumler. Wording for class template argument deduction from inherited constructors (PDF). 2022-05-20 [2025-04-28]. (原始内容存档 (PDF)于2022-07-30).
- ^ Labels at the end of compound statements (C compatibility) (PDF). 2022-01-13 [2025-04-28]. (原始内容存档 (PDF)于2022-06-17).
- ^ Jens Maurer. Extend init-statement to allow alias-declaration. 2021-04-13 [2025-04-28]. (原始内容存档于2026-05-19).
- ^ JeanHeyd Meneide; Rein Halbersma. Literal Suffix for (signed) size_t. 2019-11-24 [2025-04-28]. (原始内容存档于2026-04-29).
- ^ Extended floating-point types and standard names. 2022-04-22 [2025-04-28]. (原始内容存档于2022-07-29).
- ^ Alex Christensen; JF Bastien. P1102R2: Down with ()!. 2020-12-11 [2025-04-28]. (原始内容存档于2026-05-26).
- ^ Attributes on Lambda-Expressions (PDF). [2025-04-28]. (原始内容存档 (PDF)于2022-08-22).
- ^ Ville Voutilainen. Non-literal variables (and labels and gotos) in constexpr functions. 2021-07-12 [2025-04-28]. (原始内容存档于2026-05-02).
- ^ Permitting
static constexprvariables inconstexprfunctions. 2022-11-07 [2025-04-28]. (原始内容存档于2022-12-19). - ^ Relaxing some constexpr restrictions. 2022-01-27 [2025-04-28]. (原始内容存档于2022-07-25).
- ^ Andrzej Krzemieński. Narrowing contextual conversions to bool. 2021-04-12 [2025-04-28]. (原始内容存档于2026-05-14).
- ^ Corentin Jabot. Trimming whitespaces before line splicing (PDF). 2021-04-13 [2025-04-28]. (原始内容存档 (PDF)于2022-05-24).
- ^ Pal Balog. Make declaration order layout mandated (PDF). 2021-04-02 [2025-04-28]. (原始内容存档 (PDF)于2022-05-24).
- ^ Delimited escape sequences (PDF). 2022-02-25 [2025-04-28]. (原始内容存档 (PDF)于2022-05-24).
- ^ Named universal character escapes. 2022-03-25 [2025-04-28]. (原始内容存档于2022-07-29).
- ^ Support for UTF-8 as a portable source file encoding (PDF). 2022-07-01 [2025-04-28]. (原始内容存档 (PDF)于2026-04-19).
- ^ Corentin Jabot. Consistent character literal encoding (PDF). 2021-09-14 [2025-04-28]. (原始内容存档 (PDF)于2026-04-11).
- ^ Jens Maurer. Character sets and encodings. 2021-09-21 [2025-04-28]. (原始内容存档于2025-04-28).
- ^ Standard Library Modules std and std.compat (PDF). [2025-04-28]. (原始内容存档 (PDF)于2022-11-18).
- ^ (std::generator: Synchronous Coroutine Generator for Ranges) (PDF). [2025-04-28]. (原始内容存档 (PDF)于2022-11-30).
- ^ Vicente Botet; JF Bastien; Jonathan Wakely. std::expected. 2022-01-07 [2025-04-28]. (原始内容存档于2026-07-10).
- ^ Sy Brand. Monadic operations for std::optional. 2021-04-27 [2025-04-28]. (原始内容存档于2025-11-25).
- ^ Jeff Garland. P2505R5 Monadic Functions for
std::expected. 2022-09-28 [2025-04-28]. (原始内容存档于2022-11-17). - ^ JeanHeyd Meneide. std::to_underlying for enumerations. 2021-01-22.
- ^ Matt Calabrese; Ryan McDougall. move_only_function. 2021-07-09 [2025-04-28]. (原始内容存档于2026-07-10).
- ^ Gašper Ažman. std::forward_like (PDF). 2022-05-13 [2025-04-28]. (原始内容存档 (PDF)于2022-11-30).
- ^ Zhihao Yuan. invoke_r. 2020-04-29 [2025-04-28]. (原始内容存档于2026-05-20).
- ^ 46.0 46.1 Barry Revzin. Pipe support for user-defined range adaptors. 2021-12-17.
- ^ Isabella Muerte; Corentin Jabot. Byteswapping for fun&&nuf. 2021-09-17 [2025-04-28]. (原始内容存档于2025-07-09).
- ^ Melissa Mears; Jens Maurer. Function to mark unreachable code (PDF). 2021-10-15 [2025-04-28]. (原始内容存档 (PDF)于2026-04-15).
- ^ Corentin Jabot. Compatibility between tuple, pair and tuple-like objects (PDF). 2022-07-15 [2025-04-28]. (原始内容存档 (PDF)于2022-11-20).
- ^ Hui Xie; S. Levent Yilmaz; Tim Song. common_reference_t of reference_wrapper Should Be a Reference Type. 2023-02-07 [2025-04-28]. (原始内容存档于2023-03-09).
- ^ Wording for P2644R1 Fix for Range-based for Loop. 2022-11-11 [2025-04-28]. (原始内容存档于2022-12-19).
- ^ Peter Dimov. Making std::type_info::operator== constexpr. 2021-05-01 [2025-04-28]. (原始内容存档于2026-07-02).
- ^ Daniil Goncharov. A more constexpr bitset (PDF). 2022-06-25 [2025-04-28]. (原始内容存档 (PDF)于2022-11-20).
- ^ Andreas Fertig. Making std::unique_ptr constexpr (PDF). 2021-11-06 [2025-04-28]. (原始内容存档 (PDF)于2026-03-18).
- ^ Edward J. Rosten; Oliver J. Rosten. constexpr for <cmath> and <cstdlib> (PDF). 2021-11-12 [2025-04-28]. (原始内容存档 (PDF)于2026-05-20).
- ^ Daniil Goncharov; Alexander Karaev. Add Constexpr Modifiers to Functions to_chars and from_chars for Integral Types in <charconv> Header (PDF). 2021-09-18 [2025-04-28]. (原始内容存档 (PDF)于2022-11-20).
- ^ Juan Alday. A proposal for a type trait to detect scoped enumerations (PDF). 2020-10-12 [2025-04-28]. (原始内容存档 (PDF)于2026-05-12).
- ^ Timur Doumler; Vittorio Romeo. A trait for implicit lifetime types (PDF). 2022-11-11 [2025-04-28]. (原始内容存档 (PDF)于2023-03-09).
- ^ Tim Song. A type trait to detect reference binding to temporary. 2021-10-13 [2025-04-28]. (原始内容存档于2026-03-11).
- ^ Justin Bassett. Move-only types for equality_comparable_with, totally_ordered_with, and three_way_comparable_with (PDF). 2022-07-02 [2025-04-28]. (原始内容存档 (PDF)于2022-11-20).
- ^ Corentin Jabot; Eric Niebler; Casey Carter. Conversions from ranges to containers (PDF). 2022-01-21.
- ^ Christopher Di Bella. starts_with and ends_with. 2021-02-19.
- ^ Christopher Di Bella. std::ranges::contains.
- ^ Zach Laine. find_last (PDF). 2022-06-17.
- ^ Tim Song. ranges::iota, ranges::shift_left, and ranges::shift_right. 2021-12-05.
- ^ Barry Revzin. ranges::fold. 2022-04-22.
- ^ Tim Song. zip. 2021-06-11.
- ^ Barry Revzin. views::join_with. 2022-01-28.
- ^ Tim Song. Windowing range adaptors: views::chunk and views::slide. 2021-12-05.
- ^ Tim Song. views::chunk_by. 2021-11-19.
- ^ Barry Revzin. views::as_rvalue. 2022-02-14.
- ^ 72.0 72.1 Barry Revzin. cbegin should always return a constant iterator. 2022-06-17.
- ^ Michał Dominiak. views::repeat. 2022-07-13.
- ^ Christopher Di Bella; Tim Song. stride_view. 2022-07-08.
- ^ Sy Brand; Michał Dominiak. views::cartesian_product. 2022-07-13.
- ^ Corentin Jabot. views::enumerate (PDF). 2022-12-07.
- ^ David Olsen. Ranges iterators as inputs to non-Ranges algorithms. 2022-04-22.
- ^ David Olsen. Ranges iterators as inputs to non-Ranges algorithms. 2022-04-22.
- ^ Michał Dominiak. Relaxing range adaptors to allow for move only types. 2022-07-13.
- ^ Ville Voutilainen. Making multi-param constructors of views explicit. 2022-11-12.
另见
[编辑]| 这是一篇關於電腦程式語言的小作品。您可以通过编辑或修订扩充其内容。 |