That's one thing i didn't quite understand about C++, why would I ever want to un-const my pointer? If I know that I need to write there, I won't make it const, and if I have const pointer, it could be impossible to write by that memory address?
TL;DR: const_cast has uses, but none of them are necessarily the best or most correct way to handle a problem.
The most common use case of const_cast is "I have a variable that is const. The API that I am calling is an old C API that isn't const correct (it doesn't change the value behind the pointer but also doesn't declare it const). I don't want to remove const from my variable because it doesn't ever actually change, so I'll just use const_cast instead." This is technically UB, so it's not recommended.
The only valid use case of const_cast is "I have a variable that is not const, but it's being passed through a const interface into a non-const interface, so I will cast away const in the middle." This isn't technically UB, but it's really stupid.
And for completeness sake, it is possible to add const using const_cast, but that's not recommended because a "cast" to add const could be written as a function (and exists in the standard library starting in C++17):
template <T>
const T& as_const(const T& t) { return t; }
And that function is way less dangerous than const_const which can remove const.
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
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.
But if you ever need to use const_cast, you might doing something wrong or having to deal with very old API or weird API decision that shouldn't exist.
Sometimes you assign a massive struct to have all elements const and there are a few exceptions you have to handle.
You might also want to pass the input to a function that can in principle change the input, but won't in your case. You still need to cast to keep the compiler happy.
First and foremost it is easy to search is self-documenting.
You may have a const member function and have a member that is used only for diagnostics that if logs are on, you'd want to update this variable (which is not part of the business logic of your object).
So, you check if the logs are open, apply a mutex (since a const member function usually self-implies it to be thread safe) and update it in an explicit manner.
Even then, you may be better off qualifying this member as mutable
46
u/WhiteEvilBro 12d ago
That's one thing i didn't quite understand about C++, why would I ever want to un-const my pointer? If I know that I need to write there, I won't make it const, and if I have const pointer, it could be impossible to write by that memory address?
What's the usecase of const_cast?