Asking "am I running at compile time?" inside a
constexprfunction, and what's alive during constant evaluation. Header:<type_traits>· Since: C++20 (is_constant_evaluated), C++23 (if consteval), C++26 (is_within_lifetime)
A constexpr function can run in two worlds. During constant evaluation it
must stay inside the rules (no reinterpret_cast, no intrinsics, no I/O); at
run time it may use whatever is fastest. These tools let one function pick a
branch per world:
constexpr int f(int x) ──┬── constant evaluation ──► portable, constexpr-safe path
└── run time ─────────────► intrinsics, SIMD, logging...
constexpr bool std::is_constant_evaluated() noexcept; // C++20
if consteval { /* compile time */ } else { /* run time */ } // C++23, a statement
template<class T>
consteval bool std::is_within_lifetime(const T* p) noexcept; // C++26if consteval(C++23) for new code: it's a language statement, so the compile-time branch may callconstevalfunctions and you can't misuse it withif constexpr. See selection statements.std::is_constant_evaluated()when you must support C++20, or need the answer as aboolvalue.std::is_within_lifetime(p)inside a compile-time branch, to ask whether*pis alive, for example which union member is active.
It returns true when the call is part of a constant evaluation, and false
at run time:
constexpr int where() {
if (std::is_constant_evaluated()) return 1; // compile time
else return 2; // run time
}
void demo() {
constexpr int c = where(); // 1: a constexpr initializer is constant-evaluated
int r = where(); // 2: an ordinary local runs at run time
static int s = where(); // 1: static storage tries constant initialization first
use(c, r, s);
}The compile-time branch of if consteval is an immediate-function context, so
it can call a consteval function with a non-constant parameter:
consteval int sq(int x) { return x * x; }
constexpr int sq_or(int x) {
if consteval { return sq(x); } // OK: x is a constant here
else { return x * x; }
}
static_assert(sq_or(4) == 16);MSVC needs VS 2026 (19.5x) for this: VS 2022 (19.44) rejects the sq(x) call
inside if consteval (C7595), as a
consented windows-msvc run showed.
A plain if (std::is_constant_evaluated()) doesn't give that, so the same call
is an error:
consteval int sq(int x) { return x * x; }
constexpr int f(int x) {
if (std::is_constant_evaluated()) return sq(x); // error: x isn't a constant expression
return x * x;
}Reading an inactive union member during constant evaluation is an error, so a
compact optional can't test its own state by reading. is_within_lifetime
answers without reading. It's consteval, so it goes in an if consteval
branch. Of the local toolchains only Clang 22 (libc++) has it
(__cpp_lib_is_within_lifetime == 202306L, P2641); GCC 15 and 16 don't.
union OptBool {
char none = 2; // sentinel byte: no value
bool b;
constexpr OptBool() = default;
constexpr OptBool(bool v) : b(v) {}
constexpr bool has_value() const {
if consteval { return std::is_within_lifetime(&b); } // is b the active member?
else { return none != 2; } // run time: check the byte
}
};
static_assert(!OptBool{}.has_value());
static_assert(OptBool{false}.has_value()); // engaged, even though the value is falseif constexpr (std::is_constant_evaluated())is always true. The condition ofif constexpris itself constant-evaluated, so the run-time branch is discarded. Use plainif, or better,if consteval.- Constant-evaluated doesn't mean
constexprvariables only. Template arguments, array bounds,static_assert, and the initializers ofconstinit, static-storage andconstintegral variables are tried as constant evaluation too, sowhere()above returns 1 for a globalint g = where();. The optimizer folding a run-time call doesn't count: that call still seesfalse. - Keep both branches equivalent. The compiler may pick either world, and
code that behaves differently is hard to test. Unit-test the run-time branch
and
static_assertthe compile-time one. is_within_lifetimeis compile-time only. It'sconsteval; there's no run-time answer to "is this object alive?".