The following code example is taken from the book
C++23 - The Complete Guide
by Nicolai M. Josuttis,
Leanpub, 2026
The code is licensed under a
Creative Commons Attribution 4.0 International License.
// raw code
#include <iostream>
constexpr int foo()
{
if consteval {
return 42; // compile-time behavior
}
else {
return 72; // runtime behavior
}
}
int main()
{
int x = foo(); // runtime context
constexpr int c = foo(); // compile-time context
std::cout << "x: " << x << '\n';
std::cout << "c: " << c << '\n';
}