3 ms·
You can also use const_cast to remove the const-ness. You could then mutate the value. I believe that's undefined behavior territory (the mutation, at least in
by Zach_the_Lizard 3y ago
You can also use const_cast to remove the const-ness. You could then mutate the value.
I believe that's undefined behavior territory (the mutation, at least in most cases), but I'm sure someone is doing it in the wild.
- kevincox 3y agoI haven't done much C++ in a few years but IIRC you can remove const as long as the "original" value isn't const. So `const_cast((*const Foo)foo)` is fine if foo is not const.
- erik_seaberg 3y agoIsn’t the linker entitled to put a constant object in a read-only page of the binary if it doesn’t require a ctor at runtime?
- kevincox 3y agoYes. That is why global static `const`s (with exceptions like if they have mutable fields) can't legally have their `const` casted away.