3 ms·
> C++ const has no effect on optimization, since it can be casted away. Modifying an object that has had its const-ness casted away is undefined behaviour.
by charliesome 13y ago
> C++ const has no effect on optimization, since it can be casted away.
Modifying an object that has had its const-ness casted away is undefined behaviour.
- bmm6o 13y agoBut if you have a const reference to a non-const object, it's legal to use a const_cast to remove the const part. This is the more likely scenario, since most objects aren't const.
- brigade 13y agoSorry, I should have been more specific. C/C++ const has no effect on optimization, unless it's on a global/static object and the compiler can see the original declaration. I'm not going to look it up in the C++ spec, but in C it's only undefined behavior if the original object was const, and it would make sense for C++ to be the same.
- caf 13y agoIt doesn't matter whether it's static or automatic storage duration.