C++17 introduced if constexpr , allowing conditional compilation of branches based on compile-time conditions. C++20 introduced std::is_constant_evaluated() , providing a way to determine whether an expression is being evaluated in a manifestly constant-evaluated context. C++23 added if consteval , offering a more direct mechanism for distinguishing constant-evaluated execution from runtime execution. All three features are part of published C++ standards. Consider the following code, compiled with C++23: #include <type_traits> #include <iostream> constexpr int Calculate(int value) { if constexpr (std::is_constant_evaluated()) { return value * 2; } else { return value * 3; } } int main() { constexpr int compileTime = Calculate(10); int input; std::cin >> input; int runtime = Calculate(input); std::cout << compileTime << '\n'; std::cout << runtime << '\n'; } ...
C++20 introduced concepts and constrained templates, allowing overload resolution to consider not only parameter types but also constraints. Example Consider the following code: