-
Book Overview & Buying
-
Table Of Contents
Modern C++ Programming Cookbook - Third Edition
By :
constexpr functions enable the evaluation of functions at compile time, provided that all their inputs, if any, are also available at compile time. However, this is not a guarantee, and constexpr functions may also execute at runtime, as we have seen in the previous recipe, Creating compile-time constant expressions. In C++20, a new category of functions has been introduced: immediate functions. These are functions that are guaranteed to always be evaluated at compile time; otherwise, they produce errors. Immediate functions are useful as replacements for macros and may be important in the possible future development of the language with reflection and meta-classes.
Use the consteval keyword when you want to:
consteval unsigned int factorial(unsigned int const n)
{
return n > 1 ? n * factorial(n-1) : 1;
}
...