June 2025
Intermediate to advanced
1093 pages
33h 24m
English
You already know constexpr, which allows you to tell the compiler to evaluate an expression at compile time. The precomputed value is then assigned to the variable:
constexpr int val = 2*12+3*6;
In the program, the compiler will now directly insert 42 when you use val.
For methods and functions, constexpr means that, if possible, the function call is moved to compile time:
constexpr int add1(int v) { return v+1; }
This means that if you write add1(5) or something similar in the source code, the compiler can directly write 6 into the compiled program, and nothing needs to happen at runtime.
It is something special when you provide a constructor with constexpr. Only then can you use the ...
Read now
Unlock full access