3 ms·
If an object is defined as const, casting away its constness and modifying the object causes the program's behavior to be undefined. Compilers are free to take
by _kst_ 14y ago
If an object is defined as const, casting away its constness and modifying the object causes the program's behavior to be undefined. Compilers are free to take advantage of that.
- AshleysBrain 14y agoDo you have a spec reference for that? I was under the impression you could safely const_cast and modify pretty much anything. Perhaps it was just primitives like ints, or const member functions.
- mikeash 14y agoThat is true, but it's extremely rare to be able to take advantage of that. When people think of const as a potential optimization aid, it's almost always in the context of const pointers, which actually do nothing at all for optimization.