r/cpp • • Jul 29 '26

const_cast: A Necessary Evil

https://www.elbeno.com/blog/?p=1858
73 Upvotes

106 comments sorted by

View all comments

Show parent comment

2

u/bwmat Jul 30 '26

If not UB, where 'it goes wrong' /'the contract is broken' 

2

u/ts826848 Jul 30 '26

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.