const doesn't help most optimizing compilers, it only really helps prevent programmer clumsiness. Since most C/C++ compiler IR's are SSA-based all values are effectively const anyways at the level optimizations are performed whether or not they're mutable in the original source code.
Because you can do this at runtime.
I might have a different value everytime I call the function but this means that inside the fucntion the compiler knows the value can't change
const helps optimization in exactly one case: global data.
Adding const there requires the value to be a compile-time constant, and guarantees the optimizer can inline the value rather than reading from memory. Though it still can inline the value in many cases if it can prove the variable isn't modified.
Of course, this precludes conditional constructs such as the one proposed by excuse-me; that only works as a local var and the const is again irrelevant to the optimizer.
reply