Skip to content

Commit f148826

Browse files
committed
book: add Chapter 12 (C++26 preview)
Add a new bilingual Chapter 12 introducing C++26 as a forward-looking preview, with a prominent caveat that the standard is not finalized and most features are unimplemented: - Language: reflection and contracts (documented, not yet compilable), pack indexing, = delete("reason"), placeholder _ (runnable examples) - Library: std::execution and simd/inplace_vector/function_ref/linalg (documented), saturation arithmetic (runnable example) - Flags std::hive as uncertain for C++26. Four runnable examples under code/12 verified with clang++ -std=c++2c. Register in website/filter.py, update both toc.md, bump appendix order/nav, and wire prev/next links. Resolves #318
1 parent 5afb15e commit f148826

16 files changed

Lines changed: 389 additions & 9 deletions

book/en-us/11-cpp23.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ Most C++23 language features are already well supported by mainstream compilers,
183183

184184
Although C++23 does not bring the disruptive changes of C++11 or C++20, features such as `std::expected`, `std::print`, `std::mdspan`, and the explicit object parameter genuinely improve the day-to-day experience of writing C++. Together with the upcoming C++26, modern C++ continues to evolve.
185185

186-
[Table of Content](./toc.md) | [Previous Chapter](./10-cpp20.md) | [Next Chapter: Further Study Materials](./appendix1.md)
186+
[Table of Content](./toc.md) | [Previous Chapter](./10-cpp20.md) | [Next Chapter: Introduction of C++26](./12-cpp26.md)
187187

188188
## Further Readings
189189

book/en-us/12-cpp26.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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).

book/en-us/appendix1.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Appendix 1: Further Study Materials"
33
type: book-en-us
4-
order: 12
4+
order: 13
55
---
66

77
# Appendix 1: Further Study Materials
@@ -15,7 +15,7 @@ As mentioned in the introduction to this book, this book is just a book that tak
1515
- [Ulrich Drepper. What Every Programmer Should Know About Memory. 2007](https://people.freebsd.org/~lstewart/articles/cpumemory.pdf)
1616
- to be added
1717

18-
[Table of Content](./toc.md) | [Previous Chapter](./11-cpp23.md) | [Next Chapter](./appendix2.md)
18+
[Table of Content](./toc.md) | [Previous Chapter](./12-cpp26.md) | [Next Chapter](./appendix2.md)
1919

2020
## Licenses
2121

book/en-us/appendix2.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Appendix 2: Modern C++ Best Practices"
33
type: book-en-us
4-
order: 13
4+
order: 14
55
---
66

77
# Appendix 2: Modern C++ Best Practices

book/en-us/toc.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,17 @@
114114
- std::mdspan
115115
- std::flat_map and std::flat_set
116116
- Ranges additions
117+
- [**Chapter 12 Outlook: Introduction of C++26**](./12-cpp26.md)
118+
+ 12.1 Language features
119+
- Static reflection
120+
- Contracts
121+
- Pack indexing
122+
- = delete with a reason
123+
- The placeholder _
124+
+ 12.2 The standard library
125+
- std::execution
126+
- Saturation arithmetic
127+
- Other library facilities
117128
- [**Appendix 1: Further Study Materials**](./appendix1.md)
118129
- [**Appendix 2: Modern C++ Best Practices**](./appendix2.md)
119130

book/zh-cn/11-cpp23.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ C++23 的语言特性大多已被主流编译器较好地支持,但**部分库
183183

184184
C++23 虽然不像 C++11 或 C++20 那样带来颠覆性的变化,但 `std::expected``std::print``std::mdspan`、显式对象形参等特性,实实在在地改善了日常编写 C++ 的体验。结合后续的 C++26,现代 C++ 仍在持续演进。
185185

186-
[返回目录](./toc.md) | [上一章](./10-cpp20.md) | [下一章 进一步阅读的学习材料](./appendix1.md)
186+
[返回目录](./toc.md) | [上一章](./10-cpp20.md) | [下一章 C++26 简介](./12-cpp26.md)
187187

188188
## 进一步阅读的参考资料
189189

book/zh-cn/12-cpp26.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
---
2+
title: 第 12 章 展望:C++26 简介
3+
type: book-zh-cn
4+
order: 12
5+
---
6+
7+
# 第 12 章 展望:C++26 简介
8+
9+
[TOC]
10+
11+
> **重要提示**:截至本章撰写时,C++26 仍处于**最终定稿阶段**(其国际标准草案 DIS 预计于 2026 年前后完成),标准内容仍可能调整。同时,绝大多数 C++26 特性在当前主流编译器与标准库中**尚未实现或仍处于实验阶段**。因此本章是一个**前瞻性的预览**:对于编译器已经支持的特性,我们给出可运行的示例;对于尚未落地的重磅特性,我们仅介绍其设计意图与大致语法。一切请以 [cppreference: C++26](https://en.cppreference.com/w/cpp/26) 为准。
12+
13+
C++26 被许多人寄予厚望,因为它有望带来反射、契约、`std::execution` 等酝酿多年的重大特性。下面我们分别介绍。
14+
15+
## 12.1 语言特性
16+
17+
### 静态反射(Reflection)
18+
19+
反射或许是 C++26 最受期待的特性。它允许程序在**编译期**以一等公民的方式检视并操作程序自身的结构(类型、成员、枚举项等),从而以标准化的方式实现以往依赖宏或外部代码生成器才能完成的工作。其设计引入了反射运算符 `^^`(取得表示某实体的 `std::meta::info` 值)以及「拼接」语法 `[: ... :]`(将反射值重新变回代码):
20+
21+
```cpp
22+
// 概念性示例(语法以最终标准为准)
23+
constexpr auto r = ^^int; // 取得表示 int 的反射值
24+
typename [: r :] x = 42; // 通过拼接将反射值变回类型 int
25+
```
26+
27+
> 反射尚未在主流工具链中可用,上述代码目前无法编译,仅用于说明其形态。
28+
29+
### 契约(Contracts)
30+
31+
契约为函数引入了语言级的**前置条件****后置条件**与断言,用于以可被工具识别的方式表达并检查接口约定:
32+
33+
```cpp
34+
// 概念性示例
35+
int divide(int a, int b)
36+
pre(b != 0) // 前置条件
37+
post(r: r * b <= a) // 后置条件(r 为返回值)
38+
{
39+
contract_assert(b != 0); // 语句级断言
40+
return a / b;
41+
}
42+
```
43+
44+
> 契约曾在 C++20 阶段被移除,如今重新瞄准 C++26。它同样尚未在主流工具链中可用。
45+
46+
### 形参包索引
47+
48+
C++26 允许直接用 `pack...[N]` 的语法索引一个形参包中的第 N 个元素,从此不必再借助递归或 `std::tuple` 的层层展开:
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`
59+
60+
C++26 允许在删除函数时附上一条原因字符串,当该重载被选中时,编译器会把这条原因一并报告,从而给出更友好的错误信息:
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+
### 占位符 `_`
68+
69+
C++26 将 `_` 规定为**占位符名称**:它可以在同一作用域内被重复声明,用于明确表达「这个值我并不关心」,例如结构化绑定中不需要的部分:
70+
71+
```cpp
72+
auto _ = compute(); // 我不关心返回值
73+
auto _ = compute(); // C++26 允许再次以 _ 声明
74+
```
75+
76+
## 12.2 标准库
77+
78+
### `std::execution`(发送者 / 接收者)
79+
80+
`std::execution`(即提案 P2300,常被称为「发送者/接收者」(senders/receivers) 模型)为**异步与并行计算**提供了一套标准化、可组合的执行框架,被视为现代 C++ 异步编程的基石之一。
81+
82+
> 该框架尚未广泛落地于标准库实现,故本章不提供可运行示例。
83+
84+
### 饱和算术
85+
86+
C++26 在 `<numeric>` 中加入了**饱和算术**函数 `std::add_sat``std::sub_sat``std::mul_sat``std::div_sat` 以及 `std::saturate_cast`。与普通整数运算在溢出时回绕(wrap around)不同,饱和运算会在溢出时**钳制 (clamp)** 到该类型可表示的最大或最小值:
87+
88+
```cpp
89+
#include <numeric>
90+
#include <limits>
91+
92+
int hi = std::numeric_limits<int>::max();
93+
std::add_sat(hi, 1); // 结果为 INT_MAX,而非回绕为负数
94+
std::sub_sat(0, 1); // -1
95+
```
96+
97+
### 其他库设施
98+
99+
C++26 还计划带来许多新的库设施,包括:
100+
101+
- `std::inplace_vector`:一个容量固定、元素就地存储(不进行堆分配)的顺序容器;
102+
- `std::function_ref`:对可调用对象的**非拥有**轻量引用,适合用作函数形参;
103+
- `std::simd`(`<simd>`):可移植的数据并行(SIMD)类型;
104+
- 线性代数库 `std::linalg`、`std::text_encoding` 等。
105+
106+
> 这些设施在当前标准库中大多尚未实现;其中个别设施(如 `std::hive`)是否会最终纳入 C++26 仍存在不确定性,请以最终标准与 cppreference 为准。
107+
108+
## 12.3 关于标准与实现状态
109+
110+
再次强调:C++26 尚未正式发布,本章所述特性的**最终语法与是否纳入标准都可能发生变化**,且当前能够编译运行的仅是其中一小部分(如形参包索引、带原因的 `= delete`、占位符 `_`、饱和算术)。建议读者持续关注 [cppreference: C++26](https://en.cppreference.com/w/cpp/26) 与 [编译器支持情况](https://en.cppreference.com/w/cpp/compiler_support) 以获取最新进展。
111+
112+
## 总结
113+
114+
C++26 有望成为继 C++11、C++20 之后又一个里程碑式的版本——反射与契约若能如期落地,将再次深刻地改变我们编写 C++ 的方式。在它正式到来之前,保持关注、谨慎尝试,便是最好的准备。
115+
116+
[返回目录](./toc.md) | [上一章](./11-cpp23.md) | [下一章 进一步阅读的学习材料](./appendix1.md)
117+
118+
## 进一步阅读的参考资料
119+
120+
- [C++26 - cppreference](https://en.cppreference.com/w/cpp/26)
121+
- [C++ 编译器支持情况](https://en.cppreference.com/w/cpp/compiler_support)
122+
123+
## 许可
124+
125+
<a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/"><img alt="知识共享许可协议" style="border-width:0" src="https://i.creativecommons.org/l/by-nc-nd/4.0/80x15.png" /></a>
126+
127+
本教程由[欧长坤](https://github.com/changkun)撰写,采用[知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议](https://creativecommons.org/licenses/by-nc-nd/4.0/)许可。项目中代码使用 MIT 协议开源,参见[许可](../../LICENSE)。

book/zh-cn/appendix1.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: 附录 1:进一步阅读的学习材料
33
type: book-zh-cn
4-
order: 12
4+
order: 13
55
---
66

77
# 附录 1:进一步阅读的学习材料
@@ -15,7 +15,7 @@ order: 12
1515
- [Ulrich Drepper. 每位程序员都需要知道的内存知识. 2007](https://people.freebsd.org/~lstewart/articles/cpumemory.pdf)
1616
- 待补充
1717

18-
[返回目录](./toc.md) | [上一章](./11-cpp23.md) | [下一章 现代 C++ 的最佳实践](./appendix2.md)
18+
[返回目录](./toc.md) | [上一章](./12-cpp26.md) | [下一章 现代 C++ 的最佳实践](./appendix2.md)
1919

2020
## 许可
2121

book/zh-cn/appendix2.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: 附录 2:现代 C++ 的最佳实践
33
type: book-zh-cn
4-
order: 13
4+
order: 14
55
---
66

77
# 附录 2:现代 C++ 的最佳实践

book/zh-cn/toc.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,17 @@
114114
- std::mdspan
115115
- std::flat_map 与 std::flat_set
116116
- 范围库的增强
117+
- [**第 12 章 展望: C++26 简介**](./12-cpp26.md)
118+
+ 12.1 语言特性
119+
- 静态反射(Reflection)
120+
- 契约(Contracts)
121+
- 形参包索引
122+
- 带原因的 = delete
123+
- 占位符 _
124+
+ 12.2 标准库
125+
- std::execution
126+
- 饱和算术
127+
- 其他库设施
117128
- [**附录 1:进一步阅读的学习材料**](./appendix1.md)
118129
- [**附录 2:现代 C++ 的最佳实践**](./appendix2.md)
119130

0 commit comments

Comments
 (0)