Post

HN
Hacker News

C++26: Reflection, Memory Safety, Contracts, and a New Async Model

InfoQ Homepage News C++26: Reflection, Memory Safety, Contracts, and a New Async Model

The C++26 standard draft is now complete , reports Herb Sutter, long-time C++ expert and former chair of the ISO C++ standards committee. The finalized draft introduces reflection, enhances memory safety without requiring code rewrites, adds contracts with preconditions and postconditions alongside a new assertion statement, and establishes a unified framework for concurrency and parallelism.

Reflection is a mechanism that, as Sutter explains, gives developers the keys to C++ internal machinery , enabling the language to describe itself and generate code, and thus provide a solid foundation for metaprogramming. True to C++'s spirit, reflection comes with no runtime overhead. As a basic example of what reflection allows, the following code snippet shows how reflection enables a specialized syntax for declaring C++ interfaces:

Reflection can simplify C++’s future evolution by reducing the need for as many bespoke new language features, since many can now be expressed as reusable compile-time librariesβ€”faster to design, easier to test, and portable from day one.

The interface abstraction is part of cppfront, a compiler that builds to pure ISO C++ , which Sutter created to provide a faster way to experiment with proposals. It also includes abstractions like copyable for expressing a type with copy/move construction/assignment; ordered for defining a totally ordered type with operator<=> ; union for a tagged union with names; regex , and more.

Another area where C++26 aims to deliver significant improvements is memory safety . This includes out-of-the-box elimination of undefined behavior when reading uninitialized local variables, as well as bounds safety for most standard library types, including vector , span , string , string_view . These changes have been already deployed in production at Apple and Google, Sutter says, across hundreds of millions of lines of C++ code: