Skip to main content
A consteval constructor is an immediate function introduced in C++20 that strictly mandates its execution to occur exclusively during compile-time. Unlike constexpr constructors, which can execute at either compile-time or runtime depending on the context, a consteval constructor guarantees that every invocation is an immediate invocation. The constructor call is evaluated as a constant expression in an isolated compile-time context, producing a compile-time value (a prvalue) that can subsequently be used to initialize either a constexpr object or a runtime object.

Technical Characteristics

  1. Immediate Evaluation Context: A consteval constructor forces its execution into an immediate evaluation context. The entire call—including the arguments—must resolve to a valid constant expression. If a consteval constructor is invoked from within another immediate context (e.g., the body of another consteval function), the arguments passed to it can be non-constant parameters of that outer function, as evaluation is deferred to the outermost immediate invocation.
  2. Implicitly Inline: Like all consteval functions, a consteval constructor is implicitly an inline function.
  3. constexpr Requirements: A consteval constructor must satisfy all the structural requirements of a constexpr constructor. It cannot contain statements that are fundamentally incompatible with constant evaluation, such as calls to non-constexpr or non-consteval functions, or operations that invoke undefined behavior.
  4. Abstract Compile-Time this Pointer: During the constant evaluation of a prvalue, the compiler evaluates the constructor in an abstract compile-time context. The implicit this pointer points to a compile-time temporary, not the final runtime memory address. Because the evaluation is isolated from the final memory destination, the resulting compile-time evaluated object can safely initialize runtime variables or dynamically allocated memory.

Evaluation Mechanics

The compiler enforces the consteval constraint at the call site. Because a consteval constructor call is an immediate invocation, the compiler must be able to evaluate the entire initialization process at compile-time based on the provided arguments.

Mechanical Differences: constexpr vs. consteval Constructors

  • constexpr Constructor: Acts as a dual-purpose constructor. If invoked with constant expressions to initialize a constexpr object, it evaluates at compile-time. If invoked with runtime arguments, it gracefully degrades to a standard runtime constructor, executing its logic at runtime.
  • consteval Constructor: Acts as a strict compile-time gatekeeper for the constructor’s logic. It possesses no runtime fallback mechanism. The constructor’s execution must evaluate at compile-time, meaning the arguments passed to it must be valid within a constant evaluation context. Once evaluated, the resulting compile-time constant can be used in both compile-time and runtime contexts.
Tired of Poor C++ Skills? Fix That With Deep Grasping!Learn More