If the object was originally const, the UB had already occurred?
From what I understand forming a non-const pointer/reference to an actually-const object is fine. Its only the actual modification of said actually-const object that is UB.
I think your second comment is more correct; UB only occurs on the actual modification, so while calling a non-const method on an actually-const object is not itself UB that's probably around where a bugfix would land.
...Well, maybe barring some oddball setup with a non-const method that uses some other criteria to determine whether it's safe to modify whatever it's called on, but I'd hope that kind of thing is relatively rare.
View all comments
Show parent comment
6
u/usefulcat Jul 29 '26
There is at least one alternative that doesn't require const_cast:
Obviously this is a trivial and contrived example, but hopefully you can imagine more complex examples.
Personally I'd prefer to just use const_cast, as it's much simpler, but I'm concerned about the resulting UB.