Skip to main content
A constexpr variable is a strongly typed, implicitly const variable whose value is strictly evaluated and resolved during compile time. Applying the constexpr specifier guarantees that the variable’s initialization expression is a valid constant expression, meaning it contains no runtime dependencies and can be embedded directly into the compiled binary.

Core Mechanics and Compiler Rules

To successfully declare a constexpr variable, the compiler enforces strict requirements on both the type and the initialization expression:
  1. Constant Expression Initialization: The initialization expression must be a valid constant expression. This includes literal values, previously defined constexpr variables, or return values from constexpr functions evaluated with constant arguments.
  2. Literal Type Requirement: The variable’s type must satisfy the LiteralType core language named requirement. This includes scalar types (e.g., int, double, char), references, arrays of literal types, aggregate types (which do not require explicitly defined constructors), and non-aggregate user-defined class types that possess at least one constexpr constructor and a constexpr destructor (as of C++20).
  3. Implicit Constness: Declaring a variable as constexpr automatically applies the top-level const qualifier. The variable becomes immutable for its entire lifecycle.
  4. Constant Destruction (C++20): The type of a constexpr variable must have constant destruction. While constexpr destructors can perform operations like freeing dynamically allocated memory during compile-time evaluation (transient allocation), any memory allocated during the compile-time initialization of a constexpr variable must also be deallocated before the evaluation completes. Allocations cannot escape the constant evaluation context to persist to runtime.

Syntax Visualization

Mechanical Distinction: constexpr vs. const

While both specifiers enforce immutability, they operate at different phases of the compilation pipeline:
  • const dictates access semantics. It promises the compiler that the program will not modify the variable after initialization. A const variable can be initialized at runtime.
  • constexpr dictates evaluation semantics. It promises the compiler that the variable’s value is fully computable before the program ever runs.

Pointer and Reference Semantics

When applying constexpr to pointers or references, the specifier applies to the pointer/reference itself, not necessarily the object being pointed to.
Note that for a pointer or reference to be constexpr, the target object must have static storage duration (e.g., global variables or static class members) so its memory address is known to the linker at compile time.
Tired of Poor C++ Skills? Fix That With Deep Grasping!Learn More