C++ Reference ยท Keywords and Statements
The constexpr specifier
Reference for C++ constexpr: compile-time variables and functions, what is allowed in constant expressions by standard version, if constexpr and constexpr containers.
constexpr declares that a variable or function can be evaluated at compile time. A constexpr variable is implicitly const and must be initialized with a constant expression, so its value is baked into the program. A constexpr function may run at compile time when all its arguments are constants, and still works as a normal function with runtime arguments.
The set of things allowed inside constant evaluation grows with every standard: C++11 allowed a single return, C++14 added loops and local variables, C++20 added std::vector/std::string allocation that does not escape, virtual calls and try blocks, and C++23 relaxed more (such as static constexpr variables and non-literal types in unevaluated paths).
constexpr int square(int n) { return n * n; } constexpr int area = square(12); // computed by the compiler
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
constexpr type name = constant-expression; constexpr return-type function(params) { body } constexpr Class(params); // constexpr constructor if constexpr (condition) { ... } // C++17, in templates
| Form | Requirement |
|---|---|
| Variable | Literal type, initialized by a constant expression. Implicitly const. |
| Function | Parameters and return type must be literal types. At least one set of arguments must be able to produce a constant expression, though the compiler need not diagnose a violation (C++23 dropped this "ill-formed, no diagnostic required" rule). |
| Constructor | Allows objects to be created in constant expressions. |
if constexpr |
The condition must be a constant; the branch not taken is discarded and not instantiated when it depends on a template parameter. |
Usage notes
constexprdoes not force compile-time evaluation of a call. The call is evaluated at compile time only when its result is needed as a constant (aconstexprvariable, an array bound, a template argument,static_assert). Useconstevalto require it.- In constant evaluation any undefined behavior (overflow, out-of-bounds, null dereference) makes the expression not constant, so it is rejected as a compile error. This makes
constexpra good UB detector. - Allowed since C++20:
std::vector,std::stringandnew/deleteas long as the memory is freed before evaluation ends,std::is_constant_evaluated(), and virtual functions. Aconstexprvariable cannot hold such allocated memory itself. constexprfunctions are implicitlyinline. Astatic constexprdata member is implicitlyinlinesince C++17, so it needs no out-of-class definition.- Many standard facilities are
constexpr:std::array,std::string_view,<algorithm>(C++20),<numeric>,std::optionalandstd::variant. Most of<cmath>is notconstexprin the standard (GCC allows some as an extension), so do not rely on it. std::is_constant_evaluated()(C++20) andif consteval(C++23) let a function choose a different path at compile time and runtime.- Heavy compile-time work slows builds; GCC stops after its constexpr operations limit (
-fconstexpr-ops-limit). - A
constexprpointer is a constant pointer; useconstexpr const char* pfor a pointer to constant data.