4 ms·
const_cast is undefined behavior? Can you cite the spec?
by thwest 13y ago
const_cast is undefined behavior? Can you cite the spec?
- vinkelhake 13y agoSure: 7.1.5.1/4: Except that any class member declared mutable (7.1.1) can be modified, any attempt to modify a const object during its lifetime (3.8) results in undefined behavior. const_cast is not undefined behavior, but modifying a const object is.
- aristidb 13y agoconst_cast is undefined behavior if the actual object is const. If it's a mutable object that just happened to be passed as a reference you can const_cast it. Or that's how I understand it.
- ajuc 13y agoThe funny thing is - it doesn't even show a warning compiled with -Wall.
- alexchamberlain 13y agoWell, the `const_cast` usage is fine, but calling that function on an actual `const` object is not; it's a lot to ask a compiler to warn on that.