consteval specifier
The consteval specifier may be applied only to functions. It specifies that a function is a so-called immediate function and that every call to it must result in a compile-time constant. Let’s go through the following simple example:
constexpr int square(int x) {
return x*x;
}
int main() {
constexpr int arg = 2;
int ret = square(arg);
return ret;
}
If you run this example in Compiler Explorer using the x86-64 GCC 14.2 compiler, without optimization enabled, we can observe the following:
- The program returns 4.
- The resulting assembly is small, and it just moves 4 to the return register.
- Removing the
constexprspecifier from the variableargwill result in the function square being generated and a call to it in themainfunction.
Now, let’s change the function square constexpr specifier to consteval, as shown here:
consteval int square(int x) {
return x*x;
}
int main() {
constexpr int...