Skip to content
Tutorials Center
My learning

Topics

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).

C++
constexpr int square(int n) { return n * n; }
constexpr int area = square(12);          // computed by the compiler

Try it

C++ Compile-time and runtime from the same function

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.

C++ if constexpr, constexpr classes and constexpr std::string

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

C++
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

  • constexpr does not force compile-time evaluation of a call. The call is evaluated at compile time only when its result is needed as a constant (a constexpr variable, an array bound, a template argument, static_assert). Use consteval to 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 constexpr a good UB detector.
  • Allowed since C++20: std::vector, std::string and new/delete as long as the memory is freed before evaluation ends, std::is_constant_evaluated(), and virtual functions. A constexpr variable cannot hold such allocated memory itself.
  • constexpr functions are implicitly inline. A static constexpr data member is implicitly inline since 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::optional and std::variant. Most of <cmath> is not constexpr in the standard (GCC allows some as an extension), so do not rely on it.
  • std::is_constant_evaluated() (C++20) and if 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 constexpr pointer is a constant pointer; use constexpr const char* p for a pointer to constant data.