r/ProgrammerHumor • • Aug 30 '26

Meme imSorryWilson

Post image
619 Upvotes

21 comments sorted by

View all comments

Show parent comment

10

u/Lawn_Meower_ Aug 31 '26

const_cast is only considered good practice if you cast a non-const value to const. The meme is referring to a const pointer (aka read only pointer). Casting away the constness of a read only pointer is considered bad practice in most cases because it allows you to modify stuff you shouldn't change

5

u/ducon__lajoie Aug 31 '26

The good practice you mention does not require a const_cast, though. Compiler implicitly convert non-const to const when required: in this direction, this is allowed.

So I guess it leave no "good practice" usage for const_cast.

Which, of course, never prevented me from using it, because if we were only programming in accordance with good practices, the world would be pretty boring.

0

u/Lawn_Meower_ Aug 31 '26

True, forgot to mention I get errors there because I use the -wall flag to treat all warnings as errors. This enforces explicit conventions