I\'m a const fiend, and I strive to make everything as const as possible.
I\'ve tried looking at various dissassembly outputs from const and non const versions of fu
It's pretty rare for const to actually help the compiler optimize. You have to keep in mind that the const_cast
can be used anywhere to remove constness from an object (although actually modifying the resulting object isn't always well-defined, it is in some cases, and so the compiler has to be careful about assuming that just because a const object is passed to a function, it won't be modified)
Likewise, the mutable
keyword messes things up. You might pass a const object to a function, but what if that object contains a mutable
field?
The compiler has to do a lot of work to verify that it's safe to assume that an object is really constant -- and by the time it's done all this verification, the const
keyword doesn't really matter, because it could have done all the same analysis on a regular non-const object as well to determine that it isn't being modified and can be treated as constant.
I won't say there aren't a few border cases where the const keyword can enable new optimizations, but in general, const
isn't a performance consideration, but a correctness one. Use it to catch more bugs at compile-time, not to speed up your code.