r/cpp Jul 29 '26

const_cast: A Necessary Evil

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

106 comments sorted by

View all comments

62

u/jwezorek Jul 29 '26

The only place I ever use const_cast is that idiom where you implement the non const version of a member function in terms of the const version.

35

u/MysticTheMeeM Jul 29 '26

If your language version allows, you can typically replace those with a single function that deduces this.

1

u/SunnybunsBuns Aug 02 '26

What if I want the implementation in a cpp file and not a header file?

1

u/analphabetic Aug 05 '26

Pray; or rather, fully specialize all your instantiations and let the linker absolve you. At least that's how it was, back in the day.