tags:

views:

241

answers:

4

Possible Duplicate:
Constants and compiler optimization in C++

Let the holy wars begin: I've heard a number of differing opinions on the usefulness of const in C++. Of course it has uses in member function declarations, etc. But how useful is it as a modifier on variables (or rather, constants)? Does it indeed help the optimizer, if the rest of the code is left the same?

A: 

It can't hurt and in theory could allow for some optimizations so you might as well use it - don't know if any production compilers do.

Martin Beckett
+5  A: 

In general, no, it will not help the compiler. Since the const-ness can be casted away in a second in both C and C++, it'd be hard for the compiler to make the necessary assumptions about fulfilled code requirements for optimizations.

That said, const-correctness should always be used for its other benefits.

Johann Gerell
Well yes, it can be casted away, but if you do it behind the compilers back it will lead to undefined behavior. If you use normal cast then the compiler knows the exact point from which he has to drop the constness.
Let_Me_Be
+14  A: 

There are a lot of cases where the const modifier won't help the optimizer, for the simple fact that the compiler can already tell if you modified a variable or not. The biggest benefit of const, in my opinion, is that it tells the compiler whether the programmer intended to modify that variable, which is useful in finding certain types of semantic errors at compile time instead of run time. Any error you can move to compile time is a huge boost in programmer productivity.

Karl Bielefeldt
I would say that this is a little bit exaggerated. For trivial stuff yes, once you go beyond call boundary, oh well... Anyway it's very good to think about a missing const modifier as a semantic error that has to be fixed.
Let_Me_Be
+4  A: 

const does not help the optimizer.

Since const can be cast away with const_cast, it's possible to write programs that use const in a number of places, then cast it away and modify variables anyway, with defined behavior according to the standard. The compiler therefore must look at the actual code of the program to determine which variables are modified when, and it's probably pretty good at this anyway (for example it might determine a non-const variable is invariant over a certain block of code and optimize accordingly).

If the compiler blindly treated const as a guarantee that something won't change, the optimizer would break some well-formed programs.

const is a compile-time feature to help programmers write correct code, by adding some compile-time constraints, and indicating a code contract (eg. 'I promise not to change this parameter'). It has nothing to do with optimization. While invariants are important to optimizers, this has nothing to do with the const keyword.

There is one exception: objects declared with const. These cannot be modified; even if they are via casting, the behavior is undefined. There's some subtlety here:

const int ci = 5;
const_cast<int&>(ci) = 5; // undefined behavior, original object declared const

int i = 5;
const int& ci2 = i;        // cannot modify i through ci2, const reference
const_cast<int&>(ci2) = 5; // OK, original object not declared const

So when the compiler sees const int ci it probably does assume it will never, ever change, because modifying it is undefined behavior. However, chances are this isn't the bottleneck in your program, it's just a more sophisticated #define. Apart from that, const is weak - just a keyword for the type system.

AshleysBrain
Note that casting away constness on an object that is inherently const is undefined behavior.
Martin York