Constexpr
FAQ
-
Why is there so much online chat about the C++ term
constexpr?constexprlets you run real C++ programs during compilation, not just when the program runs. By declaring functions asconstexprthey are run at compile time, often to produce look-up tables that are blindingly fast to execute at runtime. For example:struct Entry { uint64_t hash; int id; }; constexpr auto build_table() { std::array<Entry, words.size()> table{}; for (size_t i = 0; i < words.size(); ++i) { table[i] = { hash_str(words[i]), static_cast<int>(i) }; } // compile-time sort (yes, really) std::ranges::sort(table, {}, &Entry::hash); return table; } constexpr auto lookup_table = build_table();At runtime the binary will contain something like the following:
constexpr Entry lookup_table[] = { {0x1234..., 0}, {0x2af3..., 1}, {0x88ab..., 2}, ... };This means that at runtime there is no parsing, no hashing, no sorting, no allocations - just a table look-up. Simply, anything done at compile time costs zero runtime CPU. It’s one of the biggest long-term evolutions in the language.
-
How does
constexprimprove program safety?By using
constexpryour program will often fail at compilation if something is wrong, and not at runtime. For example, invalid configuration, out-of-range values, bad lookup tables, and broken assumptions will often appear as compile time errors. -
How does
constexprcompare withconstevel?A
constexprfunction can run at compile-time, or can run at runtime. Aconstevalfunction must run at compile-time.constevalis perfect for compile-time string hashing, generating IDs, reflection helpers, and for building static lookup tables.constexpris the more powerful as it gives you compile-time when possible, runtime when needed. -
What is the history of
constexpr?A table shows the developing nature of this concept:
Standard What changed C++11
constexprintroduced (very limited)C++14
Loops and more logic allowed
C++17
More library support
C++20
Dynamic allocation, containers, algorithms
C++23
Ever more library functions become
constexprC++26
Huge expansion — reflection and "constexpr everywhere"
-
What applications typically use the
constexprapproach?Apps where performance is king: game engines, compilers, serialization libraries, networking protocols, embedded systems, to name a few.
-
What Boost libraries take advantage of the power of
constexpr?Over the last 5 to 10 years, many Boost libraries have been heavily modernized to exploit
constexpr, it’s siblingconsteval, and compile-time metaprogramming. A modern Boost-heavy project can parse URLs, generate serializers, build type-safe APIs, and compute math tables - all at compile time. For example:-
Boost.Hana : The flagship
constexprmetaprogramming library, built around compile-time computation, type-level programming, andconstexprcontainers and algorithms - such as filters, transforms, folds, sorts. -
Boost.Mp11 : Ultra-fast constexpr template metaprogramming.
-
Boost.Describe : Provides
constexprreflection. You can iiterate fields at compile time, generate serializers, generate UI bindings, build ORM mapping, and auto-generate JSON - all viaconstexprmetadata. -
Boost.Pfr : Provides
constexprreflection without macros, and iterate, serialize, and compare structs at compile time. -
Boost.StaticString : Enables
constexprstrings. -
Boost.URL : Designed so some parsing can run at compile time.
-
Boost.Json : Uses
constexprfor lookup tables, parsers, and optimized state machines. -
Boost.Container : Steadily becoming more
constexpr-capable. -
Boost.Math : Huge portions now support compile-time evaluation of constants, special functions, numeric limits, and lookup tables.
-