tags:

views:

191

answers:

4

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 compiler precompute that? (This example should not produce much overhead, but you get the point.)

Bonus point for pointing out location. ;)

TIA

A: 

Yes, I would expect a C++ compiler to pre-compute such values.

Justin Ethier
+2  A: 

As per http://stackoverflow.com/questions/436300/are-constant-c-expressions-evaluated-at-compile-time-or-at-runtime it will depend on your compiler.

Pieter
Nice link, although it discusses `#define` in C, whereas this question was about `const` in C++
Justin Ethier
Yes, but the actual discussion is about this question. Whether the expression ends up in your code through a preprocessor directive or not doesn't actually matter. A compiler won't see much difference, will he? :)
Pieter
+8  A: 

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.

James McNellis
It's probably worth pointing out that "constant expressions" here doesn't refer to a `const` variable, just an expression that comprises literal numbers instead of variables. It shouldn't matter to the compiler whether the variable is `const` or not, it will precompute the value either way
Michael Mrozek
Thanks for pointing out my wrong std reference :) I'm surprised to see it's not in the c++ standard. I guess _"the compiler can"_ isn't strong enough language. You get my +1 :)
Stephen
@Stephen: When cross compiling, for instance, I could see a benefit to not requiring floating point expressions to be evaluated at compile time. If the target architecture uses a different type of floating point, the compiler might end up forced to simulate the entire FPU to ensure the results are correct. At least, that seems intuitive to me; I might be completely off base.
Dennis Zickefoose
@Dennis, sorry, I didn't mean to imply that I think the standard _should_ require it, I entirely agree that there are probably cases where it shouldn't be done. The c std makes reference to the fact that the compiler _can_ evaluate it, the c++ std makes no reference to it at all. I just found it surprising.
Stephen
+2  A: 

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.

Thomas Matthews