C++
x
1
consteval int sqr(int n) { return n*n; }
2
constexpr int r = sqr(100); // Okay.
3
int x = 100; int r2 = sqr(x); // Error: Call does not produce a constant
4
$ clang++ prog.cc -Wall -Wextra -std=c++2a
Start
prog.cc:3:23: error: call to consteval function 'sqr' is not a constant expression int x = 100; int r2 = sqr(x); // Error: Call does not produce a constant ^ prog.cc:3:27: note: read of non-const variable 'x' is not allowed in a constant expression int x = 100; int r2 = sqr(x); // Error: Call does not produce a constant ^ prog.cc:3:5: note: declared here int x = 100; int r2 = sqr(x); // Error: Call does not produce a constant ^ 1 error generated.
1
Finish