constexpr is a specifier keyword in the C and C++ programming languages which, roughly speaking, specifies that something may be evaluated at compile time. Unlike the usual const, which specifies read-only access, constexpr is a stronger form that implies const (in most contexts). While in C this is limited only to objects, constexpr may be used on objects, functions, or structured bindings in C++. Additional keywords in the C++ family of "const" keywords include consteval, used for guaranteeing a function executes at compile time and compile time only, and constinit, used for guaranteeing static/thread storage duration, ensuring compile-time initialization (but not immutability).
Background The word constexpr is a portmanteau of constant expression, first introduced in C++11. Prior to the introduction of constexpr, options for specifying compile-time constants and functions in C++ were limited to preprocessor macros (such as #define PI 3.1415926535 or procedural macros like #define SQUARE(x) ((x) * (x))), which performed textual substitution and thus lacked the type safety of a core language feature, or declaring an enumerated constant (such as enum { WINDOW_SIZE = 600 };), which was limited only to integral types. A particular motivation for the development of compile-time expressions was that std::numeric_limits<T>::max(), although being equivalent to the macro INT_MAX, produced a non-constant expression. Initial proposals for generalised constant expressions and compile-time expressions were first proposed for inclusion into C++ by Gabriel Dos Reis in 2003, proposing generalizing constant expressions to include constant-valued functions, functions which could be evaluated at compile time, with a particular goal being to improve type-safety and portability. This proposed was further revised with the co-authorship of Bjarne Stroustrup and Jens Maurer in 2006 to introduce the constexpr keyword, along with including user-defined literals (objects of user-defined types with constexpr constructors and destructors) into the definition of constant expressions., and adopted into the draft for C++11 in July 2007. Herb Sutter, chairman of the C++ committee, in 2026 described the adoption of constexpr as one of the most 'momentous' polls of C++ history. C++20 further introduced two keywords of the "const family": consteval and constinit. With introduction of compile-time reflection in C++26, a proposal was made for consteval variables, but was not added; this was later added in C++29. constexpr was later introduced into C, beginning in C23.
Semantics The primary usage of constexpr is to allow for evaluating complex calculations at compile time, avoiding runtime overhead such as additional stack frames or memory without relying on preprocessor directives, as well as preventing certain cases of undefined behavior.
Variables A variable or a variable template may be declared constexpr if it is a definition, is a literal type, is initialized at declaration, and is constant-initializable. Such a constexpr variable is implicitly made const. References may also be made constexpr provided they are initialized by constant expression and any implicit conversions invoked during initialization are also constant expressions.
C++26 allowed constexpr structured bindings, which may bind to aggregate types (i.e. structs), arrays, and pairs/tuples.
The reflection object std::meta::info, which behaves as a mirror object, may exist only as a compile-time (i.e. constexpr) object. While it may exist as constinit, this is often useless as without immutability the compiler cannot splice the object.
Pointers and references In C, pointers (except nullptr), variably modified types, atomic types and volatile types may not be constexpr. In C++, constexpr pointers and references are subject to slightly different restrictions than regular constexpr variables. In constexpr pointers, the pointer's held address must be an address of a variable with static storage duration, and is a constant pointer. Pointer arithmetic on these addresses (if not an array) and addresses of stack variables are not constant expressions.
Meanwhile, constexpr references bind only either to variables of static storage duration or constexpr variables (regardless of storage duration).
Functions A constexpr function is a function whose return value may be computed at compile time (i.e. when the arguments are constant expressions), and when called at runtime, behaves as a regular function. A constexpr function evaluated at compile time will halt compilation once undefined behavior is invoked. constexpr functions are implicitly inline.
Because the return types of constexpr functions may be known at compile time, they may be invoked at compile time for use in compile-time expressions.
Coroutines may not be constexpr. Historically, constexpr functions had far more restrictions, including the disallowing of multiple return statements, try blocks, and inline assembly blocks, all of which have been gradually relaxed with each revision. C++17 allowed closure types and lambda functions to be used in constant evaluation content. Class methods and overloadable operators may also be declared with constexpr. Prior to C++14, constexpr methods had implicit const access, preventing them from being able to manipulate the class. Prior to C++20, virtual functions could not be constexpr.
… excerpt ends here. Continue reading the full article.