C++26: More constexpr in the Core Language -- Sandor Dargo
In this article, we review how
constexpr evolves in the C++26 core language. We are getting constexpr cast from void*, placement new, structured bindings and even exceptions (not discussed today).
C++26: More constexpr in the Core Language
by Sandor Dargo
From the article:
Since
constexprwas added to the language in C++11, its scope has been gradually expanded. In the beginning, we couldn’t even useif,elseor loops, which were changed in C++14. C++17 added support forconstexprlambdas. C++20 added the ability to use allocation and usestd::vectorandstd::stringin constant expressions. In this article, let’s see how constexpr evolves with C++26. To be more punctual, let’s see what language features become moreconstexpr-friendly. We’ll discuss library changes in a separate article, as well asconstexprexceptions, which need both language and library changes.P2738R1:
constexprcast fromvoid*Thanks to the acceptance of P2738R1, starting from C++26, one can cast from
void*to a pointer of typeTin constant expressions, if the type of the object at that adress is exactly the type ofT.Note that conversions to interconvertible - including pointers to base classes - or not related types are not permitted.
The motivation behind this change is to make several standard library functions or types work at compile time. To name a few examples: std::format, std::function, std::function_ref, std::any. The reason why this change will allow many more for more
constexprin the standard library is that storingvoid*is a commonly used compilation firewall technique to reduce template instantiations and the number of symbols in compiled binaries.

Registration is now open for CppCon 2025! The conference starts on September 15 and will be held
Registration is now open for CppCon 2025! The conference starts on September 15 and will be held
Modern C++ offers a variety of ways to work with key-value data structures like
Registration is now open for CppCon 2025! The conference starts on September 15 and will be held