Namespaces
Variants
Views
Actions

std::is_constant_evaluated

From cppreference.com
< cpp‎ | types
 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)   
(C++20)
Swap and type operations
(C++14)
(C++11)

(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)

Elementary string conversions
(C++17)
(C++17)

Stacktrace
 
Type support
Basic types
Fundamental types
Fixed width integer types (C++11)
Numeric limits
C numeric limits interface
Runtime type information
Type traits
Type categories
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Type properties
(C++11)
(C++11)
(C++14)
(C++11)

(C++11)(until C++20)
(C++11)(deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Constant evaluation context
is_constant_evaluated
(C++20)
Supported operations
Relationships and property queries
Type modifications
(C++11)(C++11)(C++11)
Type transformations
(C++11)
(C++11)
(C++17)

(C++11)(until C++20)(C++17)
 
Defined in header <type_traits>
constexpr bool is_constant_evaluated() noexcept;
(since C++20)

Detects whether the function call occurs within a constant-evaluated context. Returns true if the evaluation of the call occurs within the evaluation of an expression or conversion that is manifestly constant-evaluated; otherwise returns false.

While testing whether initializers of following variables are constant initializers, compilers may first perform a trial constant evaluation of the initializers:

  • variables with reference type or const-qualified integral or enumeration type
  • static and thread local variables

It is not recommended to depend on the result in this case.

int y;
const int a = std::is_constant_evaluated() ? y : 1;
// Trial constant evaluation fails. The constant evaluation is discarded.
// Variable a is dynamically initialized with 1
 
const int b = std::is_constant_evaluated() ? 2 : y;
// Constant evaluation with std::is_constant_evaluation() == true succeeds.
// Variable b is statically initialized with 2

Contents

[edit] Parameters

(none)

[edit] Return value

true if the evaluation of the call occurs within the evaluation of an expression or conversion that is manifestly constant-evaluated; otherwise false.

[edit] Possible implementation

// This implementation requires C++23 if consteval.
constexpr bool is_constant_evaluated() noexcept
{
    if consteval {
        return true;
    }
    else {
        return false;
    }
}

[edit] Notes

When directly used as the condition of static_assert declaration or constexpr if statement, std::is_constant_evaluated() always returns true.

Because if consteval is absent in C++20, is_constant_evaluated is typically implemented by a compiler extension.

[edit] Example

#include <type_traits>
#include <cmath>
#include <iostream>
 
constexpr double power(double b, int x)
{
    if (std::is_constant_evaluated() && !(b == 0.0 && x < 0)) {
        // A constant-evaluation context: Use a constexpr-friendly algorithm.
        if (x == 0)
            return 1.0;
        double r = 1.0, p = x > 0 ? b : 1.0 / b;
        auto u = unsigned(x > 0 ? x : -x);
        while (u != 0) {
            if (u & 1) r *= p;
            u /= 2;
            p *= p;
        }
        return r;
    } else {
        // Let the code generator figure it out.
        return std::pow(b, double(x));
    }
}
 
int main()
{
    // A constant-expression context
    constexpr double kilo = power(10.0, 3);
    int n = 3;
    // Not a constant expression, because n cannot be converted to an rvalue
    // in a constant-expression context
    // Equivalent to std::pow(10.0, double(n))
    double mucho = power(10.0, n);
 
    std::cout << kilo << " " << mucho << "\n"; // (3)
}

Output:

1000 1000

[edit] See also

constexpr specifier(C++11) specifies that the value of a variable or function can be computed at compile time[edit]
consteval specifier(C++20) specifies that a function is an immediate function, that is, every call to the function must be in a constant evaluation[edit]
constinit specifier(C++20) asserts that a variable has static initialization, i.e. zero initialization and constant initialization[edit]