Playing around with the Compiler Explorer, I can see that GCC can convert even slightly complex functions into constant values at compile time. For example:
int func2(int x, y) { return x ^ y; } int func(int x, int y) { int i,j; int k=0; for (i=0; i<x; i++) for (j=0; j<y; j++) { k += i*j + func2(i,j); } return k; } int main() { int x; x = func(4, 7); return x; } Simply inserts the answer 216 into main's return value. Note that I didn't use the keyword constexpr here. The compiler worked it out itself.
However, according to a few web pages I've read about constexpr, its purpose is to inform the compiler that a compile-time optimisation is possible here, so that the compiler actually bothers to do the calculation at compile time, whereas without the keyword, it wouldn't.
According to this answer on Stackoverflow:
The primary usage of constexpr is to declare intent.
Question:
Does constexpr ever actually change the compiler's output, causing it to calculate something at compile time that it otherwise wouldn't?
constexprexists to allow you to generate values which can be consumed by compile-time-only processes (like non-type template parameters). It may be used for other stuff, but that specifically is why it needs to be a language feature.int func2(int x, y)... I'm surprised this code compiles.