|
| 1 | +--- |
| 2 | +title: "Chapter 12: Outlook: Introduction of C++26" |
| 3 | +type: book-en-us |
| 4 | +order: 12 |
| 5 | +--- |
| 6 | + |
| 7 | +# Chapter 12: Outlook: Introduction of C++26 |
| 8 | + |
| 9 | +[TOC] |
| 10 | + |
| 11 | +> **Important note**: at the time of writing, C++26 is still being **finalized** (its Draft International Standard is expected around 2026), and its contents may still change. Moreover, the vast majority of C++26 features are **not yet implemented, or are still experimental**, in current mainstream compilers and standard libraries. This chapter is therefore a **forward-looking preview**: for features a compiler already supports we give runnable examples; for the headline features that have not yet landed we only describe their intent and approximate syntax. Treat [cppreference: C++26](https://en.cppreference.com/w/cpp/26) as the source of truth. |
| 12 | +
|
| 13 | +C++26 is highly anticipated because it is expected to deliver long-incubated major features such as reflection, contracts, and `std::execution`. We introduce them below. |
| 14 | + |
| 15 | +## 12.1 Language features |
| 16 | + |
| 17 | +### Static reflection |
| 18 | + |
| 19 | +Reflection may be the most anticipated feature of C++26. It lets a program inspect and manipulate its own structure (types, members, enumerators, etc.) as first-class values **at compile time**, providing a standardized way to do what previously required macros or external code generators. The design introduces the reflection operator `^^` (which yields a `std::meta::info` value representing an entity) and the "splice" syntax `[: ... :]` (which turns a reflection value back into code): |
| 20 | + |
| 21 | +```cpp |
| 22 | +// Conceptual example (final syntax subject to the standard) |
| 23 | +constexpr auto r = ^^int; // a reflection value representing int |
| 24 | +typename [: r :] x = 42; // splice it back into the type int |
| 25 | +``` |
| 26 | + |
| 27 | +> Reflection is not yet available in mainstream toolchains; the code above does not currently compile and only illustrates the shape of the feature. |
| 28 | +
|
| 29 | +### Contracts |
| 30 | + |
| 31 | +Contracts add language-level **preconditions**, **postconditions**, and assertions to functions, expressing and checking interface obligations in a tool-recognizable way: |
| 32 | + |
| 33 | +```cpp |
| 34 | +// Conceptual example |
| 35 | +int divide(int a, int b) |
| 36 | + pre(b != 0) // precondition |
| 37 | + post(r: r * b <= a) // postcondition (r is the return value) |
| 38 | +{ |
| 39 | + contract_assert(b != 0); // statement-level assertion |
| 40 | + return a / b; |
| 41 | +} |
| 42 | +``` |
| 43 | +
|
| 44 | +> Contracts were removed during the C++20 cycle and now target C++26. They are likewise not yet available in mainstream toolchains. |
| 45 | +
|
| 46 | +### Pack indexing |
| 47 | +
|
| 48 | +C++26 lets you index the N-th element of a parameter pack directly with `pack...[N]`, so you no longer need recursion or `std::tuple` unpacking: |
| 49 | +
|
| 50 | +```cpp |
| 51 | +template <typename... Ts> |
| 52 | +auto first(Ts... ts) { return ts...[0]; } |
| 53 | +
|
| 54 | +template <typename... Ts> |
| 55 | +auto last(Ts... ts) { return ts...[sizeof...(ts) - 1]; } |
| 56 | +``` |
| 57 | + |
| 58 | +### `= delete` with a reason |
| 59 | + |
| 60 | +C++26 allows a deleted function to carry a reason string, which the compiler reports when that overload is selected, producing friendlier diagnostics: |
| 61 | + |
| 62 | +```cpp |
| 63 | +void use_span(int* p, int n); |
| 64 | +void use_span(std::nullptr_t) = delete("pass a real buffer, not nullptr"); |
| 65 | +``` |
| 66 | +
|
| 67 | +### The placeholder `_` |
| 68 | +
|
| 69 | +C++26 makes `_` a **placeholder name**: it may be re-declared in the same scope, clearly expressing "I do not care about this value", for example the parts of a structured binding you do not need: |
| 70 | +
|
| 71 | +```cpp |
| 72 | +auto _ = compute(); // I don't care about the result |
| 73 | +auto _ = compute(); // C++26 allows re-declaring _ |
| 74 | +``` |
| 75 | + |
| 76 | +## 12.2 The standard library |
| 77 | + |
| 78 | +### `std::execution` (senders / receivers) |
| 79 | + |
| 80 | +`std::execution` (proposal P2300, often called the "senders/receivers" model) provides a standardized, composable execution framework for **asynchronous and parallel computation**, and is regarded as one of the cornerstones of modern C++ async programming. |
| 81 | + |
| 82 | +> This framework is not yet widely available in standard-library implementations, so this chapter does not provide a runnable example. |
| 83 | +
|
| 84 | +### Saturation arithmetic |
| 85 | + |
| 86 | +C++26 adds **saturation arithmetic** functions `std::add_sat`, `std::sub_sat`, `std::mul_sat`, `std::div_sat`, and `std::saturate_cast` to `<numeric>`. Unlike ordinary integer arithmetic, which wraps around on overflow, saturating operations **clamp** to the largest or smallest value the type can represent: |
| 87 | + |
| 88 | +```cpp |
| 89 | +#include <numeric> |
| 90 | +#include <limits> |
| 91 | + |
| 92 | +int hi = std::numeric_limits<int>::max(); |
| 93 | +std::add_sat(hi, 1); // yields INT_MAX instead of wrapping to a negative value |
| 94 | +std::sub_sat(0, 1); // -1 |
| 95 | +``` |
| 96 | +
|
| 97 | +### Other library facilities |
| 98 | +
|
| 99 | +C++26 also plans to bring many new library facilities, including: |
| 100 | +
|
| 101 | +- `std::inplace_vector`: a sequence container with fixed capacity whose elements are stored in place (no heap allocation); |
| 102 | +- `std::function_ref`: a lightweight **non-owning** reference to a callable, well suited as a function parameter; |
| 103 | +- `std::simd` (`<simd>`): portable data-parallel (SIMD) types; |
| 104 | +- the linear algebra library `std::linalg`, `std::text_encoding`, and more. |
| 105 | +
|
| 106 | +> Most of these are not yet implemented in current standard libraries; whether a few of them (such as `std::hive`) are ultimately included in C++26 remains uncertain — defer to the final standard and cppreference. |
| 107 | +
|
| 108 | +## 12.3 On the standard and implementation status |
| 109 | +
|
| 110 | +To reiterate: C++26 has not been officially released, the **final syntax and inclusion of the features described here may change**, and only a small subset (pack indexing, `= delete` with a reason, the placeholder `_`, saturation arithmetic) can currently be compiled and run. Readers are encouraged to follow [cppreference: C++26](https://en.cppreference.com/w/cpp/26) and the [compiler support page](https://en.cppreference.com/w/cpp/compiler_support) for the latest progress. |
| 111 | +
|
| 112 | +## Conclusion |
| 113 | +
|
| 114 | +C++26 is poised to be another milestone after C++11 and C++20 — if reflection and contracts land as planned, they will once again profoundly change the way we write C++. Until it officially arrives, staying informed and experimenting cautiously is the best preparation. |
| 115 | +
|
| 116 | +[Table of Content](./toc.md) | [Previous Chapter](./11-cpp23.md) | [Next Chapter: Further Study Materials](./appendix1.md) |
| 117 | +
|
| 118 | +## Further Readings |
| 119 | +
|
| 120 | +- [C++26 - cppreference](https://en.cppreference.com/w/cpp/26) |
| 121 | +- [C++ compiler support](https://en.cppreference.com/w/cpp/compiler_support) |
| 122 | +
|
| 123 | +## Licenses |
| 124 | +
|
| 125 | +<a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/"><img alt="Creative Commons License" style="border-width:0" src="https://i.creativecommons.org/l/by-nc-nd/4.0/88x31.png" /></a><br />This work was written by [Ou Changkun](https://changkun.de) and licensed under a <a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/">Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License</a>. The code of this repository is open sourced under the [MIT license](../../LICENSE). |
0 commit comments