Are 'const' variables precomputed by default in C++?
Suppose I have variables for positions like
const float latitude = 51.+11./60.+33.0461/3600.;
const float longitude = 12.+50./60.+31.9369/3600.;
and use them frequently in the program. Does the com开发者_StackOverflow中文版piler precompute that? (This example should not produce much overhead, but you get the point.)
Bonus point for pointing out location. ;)
TIA
I don't believe it is required for the compiler to compute the result of arithmetic constant expressions in general.
The compiler is, however, required to compute the result of an integral constant expression (basically, a constant expression composed only of integers and other values converted to integers) in cases where it needs the result--that is, when an integral constant expression is used as an array size, as a case
expression, as an enumerator value, etc.
I'd be surprised, however, if any modern compiler didn't compute the result of a constant expression.
As per Are constant C expressions evaluated at compile time or at runtime? it will depend on your compiler.
There are two separate issues with your query: constness and constant arithmetic expressions. The constness issue comes up because you used the const
modifier. Your question involves both.
Constant Arithmetic (or textual) Expressions
The compiler may precompute (evaluate) expressions that have no variables before generating any intermediate code or final assembly code. The compiler may implement an assignment of a constant expression in any method, usually for optimization, as log as final result of the generated code is equal to the expression.
For example, some processors can load a value into a register by using shifts and adds faster than it can by loading a value from a location in the executable. The ARM processor is one of these. Thus the value 29 would be more efficient to load using shifts than to load from a location.
A variable definition does not have to be const
in order for the compiler to evaluate and "precompute" a constant arithmetic expression.
The const
Modifier
Using const
in a variable declaration or definition informs the compiler that the variable will not be modified before the end of the current scope. Any modifications to the variable, from assignments to use as parameters to functions, shall generate error messages to the user.
The 'const' variable definition need not consist only of constant arithmetic or textual expressions. For example:
void function (const std::string text)
{
const std::string::size_type extra_length(text.length() + 5);
std::cout << "Extra text length: " << extra_length << "\n";
return;
}
In the above example, the variable extra_length
cannot be "precomputed" since it depends on the function parameter, yet it is declared as const
.
Summary
Evaluation of constant arithmetic or textual expressions are a separate concept from usage of the const
modifier. Most compilers will evaluate and simplify constant arithmetic expressions before applying their values to the code generator. The value of the expression may not appear in the generated code since the compiler is allowed to generate code that computes the value as long as the computed value is equal to the value of the arithmetic expression. This may be performed as a technique for generating optimal executable code.
Yes, I would expect a C++ compiler to pre-compute such values.
精彩评论