1. Problem It Solves
Code sometimes needs one implementation path during constant evaluation and another at runtime, while still presenting one function interface. It makes an important assumption visible and checkable.
2. Prerequisites
constexpr functions, arrays, and compile-time assertions.
You should be able to compile a short program and read its output.
3. Core Idea
std::is_constant_evaluated() is a sensor inside a constexpr function. It reports which evaluation world the current call inhabits. Read std::is_constant_evaluated as a precise promise; runtime preconditions still belong to the programmer.
4. Minimal Syntax
if (std::is_constant_evaluated()) { return value + 1; }5. How It Works
The program introduces the smallest relevant form of
std::is_constant_evaluated.It applies the feature to fixed data while required owners remain in scope.
It prints one result that can be checked against the source.
6. Common Mistakes
Testing it in a context that is itself manifestly constant-evaluated can produce a predictably true result and may surprise code written as a runtime probe.
Also check the required header, C++20 library support, lifetime, and deduced types.
7. When to Use It
Use it when a constexpr-capable algorithm needs a legal compile-time path and an optimized or instrumented runtime path.
Avoid it when both contexts can use the same simple implementation.
8. Simple Example
One function returns different offsets in constant and runtime evaluation, and a constexpr std::array verifies the compile-time result. The companion .cpp uses no input, so its result is easy to reproduce.
Complete sample code
Source file
cpp20/17_is_constant_evaluated_constexpr_library/main.cpp
// Day 17: is_constant_evaluated and the constexpr Standard Library
#include <array>
#include <iostream>
#include <type_traits>
constexpr int adjusted(int value) {
if (std::is_constant_evaluated()) {
return value + 1;
}
return value + 2;
}
int main() {
constexpr std::array<int, 2> values{adjusted(4), adjusted(9)};
static_assert(values[0] == 5 && values[1] == 10);
std::cout << "compile-time value = " << values[0] << '\n';
std::cout << "runtime value = " << adjusted(4) << '\n';
}
9. Key Takeaways
std::is_constant_evaluatedexpresses the central C++20 idea of this day.The example isolates one behavior with fixed data.
Compiler checks do not replace lifetime and runtime reasoning.
Prefer the smallest interface that states the real requirement.
10. Self-Check Questions
Easy — What is the main job of
std::is_constant_evaluatedin the minimal example?Medium — Why do the
static_assertvalue and the runtime printed value differ for the same numeric argument?Hard — Why should
std::is_constant_evaluated()usually be called directly in the branch condition instead of cached in a misleading context?