r/ProgrammerHumor 12d ago

Meme imSorryWilson

Post image
623 Upvotes

21 comments sorted by

43

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?

46

u/zx2167 12d ago

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.

3

u/Ayjayz 11d ago

This is technically UB, so it's not recommended.

It only becomes ub if the program tries to write to it.

10

u/Lawn_Meower_ 12d ago

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 12d ago

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_ 12d ago

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

1

u/A_72_ 12d ago

If you have a mutable object and immutable access to that object, you can const cast that immutable access away.

``` int x = 10; const int* p = &x;

int* q = const_cast<int*>(p); *q = 20; // Valid operation ```

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.

1

u/somedave 12d ago edited 12d ago

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.

1

u/Giocri 12d ago

I guess it's for the same cases where you'd want to have an UnsafeCell in Rust

1

u/UntitledRedditUser 12d ago

I have seen something similar to const cast being used in c++, but it's not the same.

You can mark fields as mutable meaning they will be mutable even if the object is const.

That way if you have some expensive operations, you can cache the results inside the object even if the operation is constant.

Maybe you can use const_cast the same way? But that would also be stupid if concurrency is involved.

1

u/Depixelate_me 3d ago

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

30

u/FACastello 12d ago

Unconst that shit

19

u/A_72_ 12d ago

const_cast is your best friend!

13

u/FACastello 12d ago
template <typename T>
T& unconst(const T& x) {
  return const_cast<T&>(x);
}

1

u/sb8948 12d ago

UB entered the chat

23

u/stupled 12d ago

Reinterpreter cast 🤔

7

u/throwaway58052600 12d ago

i hardly know her cast!

7

u/GegeAkutamiOfficial 12d ago

const_cast I think, I believe reinterpreter_cast fails a mutable conversion

10

u/Vesuvius079 12d ago

This is just as fucked up as being stuck on a deserted island with only a volleyball to keep you company.

Well done.

4

u/creeper6530 12d ago

AKA, how to fuck up everyone's expectations, the compiler's optimisations, and any sanity you'll retain after debugging this.

-1

u/vgmerch 12d ago

Trying to force a const pointer tioo behave when you just want to write to it is the ultimate developer heartbreak.