The next major release of C++ reached an important milestone earlier this month, when the ISO C++ committee froze the feature set that will go into C++26. Notable additions include compile-time reflection, contracts, asynchronous execution, and many others.
Static reflection enables compile-time introspection on types and behavior, aiming to extend C++ metaprogramming capabilities. At a basic level, reflection allows to write code similar to the following to convert an enum
to a string
:
enum Color { red, green, blue };
static_assert(enum_to_string(Color::red) == "red");
While the user-facing syntax for using reflection is simple, in keeping with modern C++ philosophy, the implementation of the feature shown above is significantly more complex. Still it offers a glimpse of the capabilities reflection will unlock:
template <typename E>
requires std::is_enum_v<E>
constexpr std::string enum_to_string(E value) {
template for (constexpr auto e : std::meta::members_of(^E)) {
if (value == [:e:]) {
return std::string(std::meta::name_of(e));
}
}
return "<unnamed>";
}
Reflection will debut in C++ in an early form and is expected to evolve in future versions. Even so, it will already be powerful enough to enable advanced use cases, such as generating bindings for other languages like JavaScript and Python. This is how Herb Sutter describes the feature in its latest Trip Report:
Even with the first partial reflection capability we have today, we will already be able to reflect on C++ types and use that information plus plain old
std::cout
to generate arbitrary additional C++ source code that is based on that information and that we can compile and link into the same program as it’s being built.
In the future, using reflection it will be possible to generate C++ code within the same source file using token injection.
Other features accepted into the standard at the latest ISO C++ committee meeting include parallel algorithms for the Ranges library, async scopes, aimed at enabling RAII (resource allocation is initialization) style programming for resource management in asynchronous contexts, and a parallel scheduler providing an advanced execution context for thread pools.
Several major features accepted in previous committee meetings include contracts, asynchronous execution, and numerous others not covered here, such as enhancements to template metaprogramming with pack indexing, the #embed
directive for including binary resources, bounds-checked iterators and null pointer validation, SIMD parallelism, and more.
Contracts aim to support design by contract through [[pre:]]
, [[post:]]
, and contract_assert
. Originally planned for C++20, they were ultimately removed from the final draft, but are now making their way into the standard.
std::execution
provides mechanisms to manage asynchronous execution on generic execution resources. Its core concepts are sender and receiver: a sender represents a unit of asynchronous work that sends its result to a receiver. The framework is completed by the concepts of state associated with an asynchronous operation and a light-weight scheduler.
Work on C++26 continues, but as Sutter highlights, it is closer to completion than many might expect, with GCC and Clang already supporting about two-thirds of the adopted language features. InfoQ will keep reporting on the evolution of the next C++ standard as it progresses.