跳至內容

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 相關改動:
    • 在 constexpr 函數中允許非字面量變量、標籤(label)與 goto 陳述式[26]
    • 允許在 constexpr 函數中使用 static 和 thread_local 變量(只要可用於常數表達式)[27]
    • constexpr 函數不再要求返回類型與參數類型是字面量類型
    • 現在可以編寫即使沒有任何呼叫符合核心常數表達式要求的 constexpr 函數[28]
  • 將 static_assert 和 if constexpr 中的上下文收窄轉換(contextual conversion)收窄到 bool[29]
  • 在行拼接(line splicing)之前修剪空白字元[30]
  • 強制聲明順序決定主記憶體布局[31]
  • 分隔的跳脫序列(delimited escape sequences)[32]
  • 命名的通用字元跳脫(named universal character escapes)[33]
  • 文字編碼相關變化:
    • 支援將 UTF-8 作為可移植的原始檔編碼[34]
    • 字元字面量編碼一致性[35]
    • 字元集與編碼相關說明[36]

標準庫

[編輯]

標準庫模組支援

[編輯]

協程庫支援

[編輯]
  • 用於範圍的同步協程 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 支援:
    • std::type_info::operator==[52]
    • std::bitset[53]
    • std::unique_ptr[54]
    • 部分 <cmath> 函數[55]
    • std::to_chars 和 std::from_chars 的整型多載[56]
  • 元程式設計工具:
    • 類型特性 std::is_scoped_enum[57]、std::is_implicit_lifetime[58]、std::reference_constructs_from_temporary 和 std::reference_converts_from_temporary[59]
  • 為比較概念添加僅抽取式類型支援[60]

迭代器、範圍和演算法支援

[編輯]

範圍轉換工具std::ranges::to[61]

  • 新的受約束範圍演算法:
    • std::ranges::starts_with
    • std::ranges::ends_with[62]
    • std::ranges::contains
    • std::ranges::contains_subrange[63]
    • std::ranges::find_last、ranges::find_last_if 与 ranges::find_last_if_not [64]
    • 範圍版本的ranges::iota,ranges::shift_left,和 ranges::shift_right[65]
    • ranges::fold_left等範圍摺疊演算法[66]
  • 新的std::ranges::range_adaptor_closure,一個用於程式定義的範圍配接器閉包的輔助類[46]
  • 新的範圍配接器:
    • std::views::zip 系列,包含 zip_transform
    • std::views::adjacent 系列,包含 adjacent_transform[67]
    • std::views::join_with[68]
    • std::views::slide
    • std::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]

註釋

[編輯]
  1. ^ ISO/IEC 14882:2024 Programming languages — C++. iso.org. [2025-03-13]. (原始內容存檔於2021-10-06). 
  2. ^ 2020 Prague Meeting Invitation and Information (PDF). [2020-09-08]. (原始內容存檔 (PDF)於2019-12-29). 
  3. ^ 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). 
  4. ^ P2145R0: Evolving C++ Remotely. www.open-std.org. [2020-09-08]. (原始內容存檔於2021-03-01). 
  5. ^ Business Plan and Convener's Report: ISO/IEC JTC1/SC22/WG21 (C++) (PDF). [2020-09-08]. (原始內容 (PDF)存檔於2021-10-23). 
  6. ^ Upcoming Meetings, Past Meetings : Standard C++. isocpp.org. [2020-09-08]. (原始內容存檔於2020-09-07). 
  7. ^ 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). 
  8. ^ Gašper Ažman; Sy Brand; Ben Deane; Barry Revzin. Deducing this. 2021-07-12 [2025-04-28]. (原始內容存檔於2026-05-21). 
  9. ^ Barry Revzin; Richard Smith; Andrew Sutton; Daveed Vandevoorde. if consteval. 2021-03-22 [2025-04-28]. (原始內容存檔於2026-06-01). 
  10. ^ Mark Hoemmen; Daisy Hollman; Corentin Jabot; Isabella Muerte; Christian Trott. Multidimensional subscript operator (PDF). 2021-09-14 [2025-04-28]. (原始內容存檔 (PDF)於2026-05-20). 
  11. ^ static operator(). 2022-04-08 [2025-04-28]. (原始內容存檔於2022-07-29). 
  12. ^ static operator[] (PDF). 2022-11-11 [2025-04-28]. (原始內容存檔 (PDF)於2026-05-24). 
  13. ^ Simpler implicit move. 2022-03-23 [2025-04-28]. (原始內容存檔於2022-05-24). 
  14. ^ Zhihao Yuan. auto(x): decay-copy in the language. 2021-07-12 [2025-04-28]. (原始內容存檔於2026-06-11). 
  15. ^ Melanie Blower. Add support for preprocessing directives elifdef and elifndef (PDF). 2021-04-30 [2025-04-28]. (原始內容存檔 (PDF)於2026-05-19). 
  16. ^ Aaron Ballman. Support for #warning (PDF). 2022-01-13 [2025-04-28]. (原始內容存檔 (PDF)於2022-07-30). 
  17. ^ Wording for P2644R1 Fix for Range-based for Loop. 2022-11-11 [2025-04-28]. (原始內容存檔於2022-12-19). 
  18. ^ Portable assumptions (PDF). 2022-04-22 [2025-04-28]. (原始內容存檔 (PDF)於2022-11-06). 
  19. ^ Timur Doumler. Wording for class template argument deduction from inherited constructors (PDF). 2022-05-20 [2025-04-28]. (原始內容存檔 (PDF)於2022-07-30). 
  20. ^ Labels at the end of compound statements (C compatibility) (PDF). 2022-01-13 [2025-04-28]. (原始內容存檔 (PDF)於2022-06-17). 
  21. ^ Jens Maurer. Extend init-statement to allow alias-declaration. 2021-04-13 [2025-04-28]. (原始內容存檔於2026-05-19). 
  22. ^ JeanHeyd Meneide; Rein Halbersma. Literal Suffix for (signed) size_t. 2019-11-24 [2025-04-28]. (原始內容存檔於2026-04-29). 
  23. ^ Extended floating-point types and standard names. 2022-04-22 [2025-04-28]. (原始內容存檔於2022-07-29). 
  24. ^ Alex Christensen; JF Bastien. P1102R2: Down with ()!. 2020-12-11 [2025-04-28]. (原始內容存檔於2026-05-26). 
  25. ^ Attributes on Lambda-Expressions (PDF). [2025-04-28]. (原始內容存檔 (PDF)於2022-08-22). 
  26. ^ Ville Voutilainen. Non-literal variables (and labels and gotos) in constexpr functions. 2021-07-12 [2025-04-28]. (原始內容存檔於2026-05-02). 
  27. ^ Permitting static constexpr variables in constexpr functions. 2022-11-07 [2025-04-28]. (原始內容存檔於2022-12-19). 
  28. ^ Relaxing some constexpr restrictions. 2022-01-27 [2025-04-28]. (原始內容存檔於2022-07-25). 
  29. ^ Andrzej Krzemieński. Narrowing contextual conversions to bool. 2021-04-12 [2025-04-28]. (原始內容存檔於2026-05-14). 
  30. ^ Corentin Jabot. Trimming whitespaces before line splicing (PDF). 2021-04-13 [2025-04-28]. (原始內容存檔 (PDF)於2022-05-24). 
  31. ^ Pal Balog. Make declaration order layout mandated (PDF). 2021-04-02 [2025-04-28]. (原始內容存檔 (PDF)於2022-05-24). 
  32. ^ Delimited escape sequences (PDF). 2022-02-25 [2025-04-28]. (原始內容存檔 (PDF)於2022-05-24). 
  33. ^ Named universal character escapes. 2022-03-25 [2025-04-28]. (原始內容存檔於2022-07-29). 
  34. ^ Support for UTF-8 as a portable source file encoding (PDF). 2022-07-01 [2025-04-28]. (原始內容存檔 (PDF)於2026-04-19). 
  35. ^ Corentin Jabot. Consistent character literal encoding (PDF). 2021-09-14 [2025-04-28]. (原始內容存檔 (PDF)於2026-04-11). 
  36. ^ Jens Maurer. Character sets and encodings. 2021-09-21 [2025-04-28]. (原始內容存檔於2025-04-28). 
  37. ^ Standard Library Modules std and std.compat (PDF). [2025-04-28]. (原始內容存檔 (PDF)於2022-11-18). 
  38. ^ (std::generator: Synchronous Coroutine Generator for Ranges) (PDF). [2025-04-28]. (原始內容存檔 (PDF)於2022-11-30). 
  39. ^ Vicente Botet; JF Bastien; Jonathan Wakely. std::expected. 2022-01-07 [2025-04-28]. (原始內容存檔於2026-07-10). 
  40. ^ Sy Brand. Monadic operations for std::optional. 2021-04-27 [2025-04-28]. (原始內容存檔於2025-11-25). 
  41. ^ Jeff Garland. P2505R5 Monadic Functions for std::expected. 2022-09-28 [2025-04-28]. (原始內容存檔於2022-11-17). 
  42. ^ JeanHeyd Meneide. std::to_underlying for enumerations. 2021-01-22. 
  43. ^ Matt Calabrese; Ryan McDougall. move_only_function. 2021-07-09 [2025-04-28]. (原始內容存檔於2026-07-10). 
  44. ^ Gašper Ažman. std::forward_like (PDF). 2022-05-13 [2025-04-28]. (原始內容存檔 (PDF)於2022-11-30). 
  45. ^ Zhihao Yuan. invoke_r. 2020-04-29 [2025-04-28]. (原始內容存檔於2026-05-20). 
  46. ^ 46.0 46.1 Barry Revzin. Pipe support for user-defined range adaptors. 2021-12-17. 
  47. ^ Isabella Muerte; Corentin Jabot. Byteswapping for fun&&nuf. 2021-09-17 [2025-04-28]. (原始內容存檔於2025-07-09). 
  48. ^ Melissa Mears; Jens Maurer. Function to mark unreachable code (PDF). 2021-10-15 [2025-04-28]. (原始內容存檔 (PDF)於2026-04-15). 
  49. ^ Corentin Jabot. Compatibility between tuple, pair and tuple-like objects (PDF). 2022-07-15 [2025-04-28]. (原始內容存檔 (PDF)於2022-11-20). 
  50. ^ 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). 
  51. ^ Wording for P2644R1 Fix for Range-based for Loop. 2022-11-11 [2025-04-28]. (原始內容存檔於2022-12-19). 
  52. ^ Peter Dimov. Making std::type_info::operator== constexpr. 2021-05-01 [2025-04-28]. (原始內容存檔於2026-07-02). 
  53. ^ Daniil Goncharov. A more constexpr bitset (PDF). 2022-06-25 [2025-04-28]. (原始內容存檔 (PDF)於2022-11-20). 
  54. ^ Andreas Fertig. Making std::unique_ptr constexpr (PDF). 2021-11-06 [2025-04-28]. (原始內容存檔 (PDF)於2026-03-18). 
  55. ^ Edward J. Rosten; Oliver J. Rosten. constexpr for <cmath> and <cstdlib> (PDF). 2021-11-12 [2025-04-28]. (原始內容存檔 (PDF)於2026-05-20). 
  56. ^ 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). 
  57. ^ Juan Alday. A proposal for a type trait to detect scoped enumerations (PDF). 2020-10-12 [2025-04-28]. (原始內容存檔 (PDF)於2026-05-12). 
  58. ^ Timur Doumler; Vittorio Romeo. A trait for implicit lifetime types (PDF). 2022-11-11 [2025-04-28]. (原始內容存檔 (PDF)於2023-03-09). 
  59. ^ Tim Song. A type trait to detect reference binding to temporary. 2021-10-13 [2025-04-28]. (原始內容存檔於2026-03-11). 
  60. ^ 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). 
  61. ^ Corentin Jabot; Eric Niebler; Casey Carter. Conversions from ranges to containers (PDF). 2022-01-21. 
  62. ^ Christopher Di Bella. starts_with and ends_with. 2021-02-19. 
  63. ^ Christopher Di Bella. std::ranges::contains. 
  64. ^ Zach Laine. find_last (PDF). 2022-06-17. 
  65. ^ Tim Song. ranges::iota, ranges::shift_left, and ranges::shift_right. 2021-12-05. 
  66. ^ Barry Revzin. ranges::fold. 2022-04-22. 
  67. ^ Tim Song. zip. 2021-06-11. 
  68. ^ Barry Revzin. views::join_with. 2022-01-28. 
  69. ^ Tim Song. Windowing range adaptors: views::chunk and views::slide. 2021-12-05. 
  70. ^ Tim Song. views::chunk_by. 2021-11-19. 
  71. ^ Barry Revzin. views::as_rvalue. 2022-02-14. 
  72. ^ 72.0 72.1 Barry Revzin. cbegin should always return a constant iterator. 2022-06-17. 
  73. ^ Michał Dominiak. views::repeat. 2022-07-13. 
  74. ^ Christopher Di Bella; Tim Song. stride_view. 2022-07-08. 
  75. ^ Sy Brand; Michał Dominiak. views::cartesian_product. 2022-07-13. 
  76. ^ Corentin Jabot. views::enumerate (PDF). 2022-12-07. 
  77. ^ David Olsen. Ranges iterators as inputs to non-Ranges algorithms. 2022-04-22. 
  78. ^ David Olsen. Ranges iterators as inputs to non-Ranges algorithms. 2022-04-22. 
  79. ^ Michał Dominiak. Relaxing range adaptors to allow for move only types. 2022-07-13. 
  80. ^ Ville Voutilainen. Making multi-param constructors of views explicit. 2022-11-12. 

另見

[編輯]