Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

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.
class CompileTimeObject {
public:
    int state;

    // consteval constructor declaration
    consteval CompileTimeObject(int val) : state(val) {}
};

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.
struct StrictConstant {
    int value;
    
    consteval StrictConstant(int v) : value(v) {}
};

// Outer immediate context
consteval StrictConstant wrapper(int v) {
    // VALID: 'v' is a function parameter, not a constant expression itself, 
    // but it is valid here because it is inside another immediate context.
    return StrictConstant(v);
}

int main() {
    // VALID: Literal '42' is a constant expression.
    // Initializes a compile-time object.
    constexpr StrictConstant a(42); 

    // VALID: 'compile_time_var' is a constant expression.
    constexpr int compile_time_var = 100;
    constexpr StrictConstant b(compile_time_var);

    // VALID: The outermost immediate invocation resolves to a constant expression.
    constexpr StrictConstant c = wrapper(200);

    // VALID: Initializes a non-constexpr runtime local variable.
    // The constructor evaluates at compile-time, producing a prvalue 
    // that is then used to initialize the runtime object 'e'.
    StrictConstant e(42); 

    // VALID: Dynamically allocates memory at runtime.
    // The constructor evaluates at compile-time, and the resulting 
    // value initializes the dynamically allocated memory.
    auto* ptr = new StrictConstant(42);

    // INVALID: 'runtime_var' is not a constant expression.
    // Compilation fails: arguments to a consteval constructor must 
    // be usable in a constant expression.
    int runtime_var = 50;
    // constexpr StrictConstant d(runtime_var); 
    // StrictConstant f(runtime_var);
}

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.
Master C++ with Deep Grasping Methodology!Learn More