I am reading Bjarne's A Tour of C++ and he has this example in section 1.6 Constants.
constexpr int dmv = 17; // dmv is a named constant
int var = 17; // var is not a constant
const double sqv = sqrt(var); // sqv is a named constant, possibly computed at run time
My question is related to his comment on the last line
... possibly computed at run time
From looking at the code, the value var
, a numeric literal of 17
isn't going to be changed anywhere in the code. Why is it not evaluated at compile time by the compiler?
I saw this question's answer when looking and it makes sense that const
is used for semantics and more for the programmer than the compiler. But my main question is:
Can the compiler not optimize this and if not is it because it is too much to ask of the compiler to optimize?
EDIT: The part of the compiler I would like to optimize is to compute sqv
since var
is not changed anywhere in the program.