Compile time optimizations follow as-if rule that says compiler can restructure the code without changing the behavior, to optimize the code.
Under this rule, compiler can evaluate an expression at compile time to save time for runtime. This is called compile time evaluation.
Compile time evaluation includes, constant folding and constant propagation.
Constant folding
Compiler can evaluate an expression composed of literals. These can be expressions or sub-expressions as shown below.
int a {2 + 1}; // will be evalauted to 3 in compiled code.
std::cout << 33 + 1; // this will be evaluated.Constant propagation
Compiler can substitute value for variables that can or may be constant. If a variable is made constant, it becomes easy for compiler to substitute them otherwise algorithms have to determine if variables are not changing in the program.
const int a {1};
std::cout << a; // this will be substituted with 1 during compilation.