C++26 的这四大特性:反射、安全、契约、并发

第一:反射(Reflection)——“代码生成代码”的终极魔法

Herb Sutter 将反射放在了四大特性之首,并称之为“自模板(Templates)发明以来 C++ 最重要的升级”

C++26 之前,如果你想实现一个通用的 JSON 序列/反序列化库,你必须写大量重复的模板代码,或者用各种丑陋的宏来“欺骗”编译器。

第二:内存安全(Memory Safety)——“只需重编,安全自来”

C++26 给出了一个极其诱人的承诺:你的老代码一行都不用改,只要用 C++26 模式重新编译,就能自动获得大幅度的安全提升!

这主要来源于两个方面的改进:

  1. 消灭未初始化变量的 UB:在 C++26 中,读取未初始化局部变量不再是“未定义行为(Undefined Behavior)”。这意味着困扰无数新手的、极其诡异的程序崩溃,将成为历史。

  2. “加固”的标准库:Google 和 Apple 已经将它们内部经过“加固(Hardened)”的标准库实现贡献给了 C++26。这意味着,当你使用 std::vectorstd::string 等容器时,大量的边界检查会自动开启。

第三:契约(Contracts)——代码里的“法律条文”

C++26 正式引入了语言级的契约编程

你可以像签合同一样,为你的函数制定严格的法律条文:

pre 和 post 是编译器和运行时可以理解并强制执行的“法律”。如果调用者违反了前置条件,程序可以在开发阶段就立刻崩溃并给出明确的报错,而不是等到数据被污染后才在某个奇怪的地方爆炸。

第四:std::execution——C++ 的“亲儿子”协程模型

在 C++20 中,虽然引入了 co_await 协程,但它只是一个语法糖,并没有提供一个统一的调度框架。

C++26 终于补上了这块短板,正式推出了 std::execution,也被称为 Sender/Receiver 模型

std::execution的示例

// This is an example of a custom algorithm for starting work
// without allocations. This algorithm is also available in
// <exec/start_now.hpp>. (Users that don't write custom sender
// algorithms will not need to use receivers or call `connect`
// or `start`.)
template <stdexec::sender_in<stdexec::empty_env> Sender>
struct start_now {
  start_now(Sender sndr)
    : _op(stdexec::connect(std::move(sndr), _sink_rcvr())) {
    stdexec::start(_op);
  }
private:
  // start_now is implemented in terms of this custom receiver,
  // which is used to discard Sender's results.
  struct _sink_rcvr {
    using receiver_concept = stdexec::receiver_t;
    void set_value(auto&&...) noexcept {}
    void set_error(auto&&) noexcept {}
    void set_stopped() noexcept {}
  };
  stdexec::connect_result_t<Sender, _sink_rcvr> _op;
};

int main() {
  // A run loop is a fifo queue of work and a loop to execute the
  // work. It needs to be driven by calling its .run() member fn.
  stdexec::run_loop ctx;
  auto event_loop = ctx.get_scheduler();

  // Create two tasks that cooperatively multitask.
  auto task1 = stdexec::just()
             | stdexec::then([]{ std::puts("hello from task 1! suspending..."); })
             | stdexec::continue_on(event_loop) // suspend
             | exec::repeat_n(5)
             | stdexec::then([]{ std::puts("task 1 is done!"); });

  auto task2 = stdexec::just()
             | stdexec::then([]{ std::puts("hello from task 2! suspending..."); })
             | stdexec::continue_on(event_loop) // suspend
             | exec::repeat_n(8)
             | stdexec::then([]{ std::puts("task 2 is done!"); });

  // Start both tasks. This enqueues them for execution on the run loop.
  auto op1 = start_now(stdexec::start_on(event_loop, std::move(task1)));
  auto op2 = start_now(stdexec::start_on(event_loop, std::move(task2)));

  ctx.finish(); // tell the run loop to stop when the queue is empty
  ctx.run();    // tell the run loop to start executing work in the queue
}

参考资料
  • https://herbsutter.com/2026/03/29/c26-is-done-trip-report-march-2026-iso-c-standards-meeting-london-croydon-uk/

  • https://herbsutter.com/2025/06/21/trip-report-june-2025-iso-c-standards-meeting-sofia-bulgaria/

  • https://herbsutter.com/2024/07/02/trip-report-summer-iso-c-standards-meeting-st-louis-mo-usa/

  • https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p2996r13.html

  • https://www.youtube.com/watch?v=7z9NNrRDHQU

  • https://www.youtube.com/watch?v=oitYvDe4nps

Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐