News for hackers. Stuff that matters.
Re:Hello. I'm a compiler by _kst_ on Tuesday February 19, 2013 @12:17PM (#5245520)
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.
Re:Hello. I'm a compiler by AshleysBrain on Tuesday February 19, 2013 @12:33PM (#5245648)
Do 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.
Re:Hello. I'm a compiler by mikeash on Tuesday February 19, 2013 @12:36PM (#5245679)
That 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.