r/ProgrammerHumor 2d ago

Meme imSorryWilson

Post image
591 Upvotes

20 comments sorted by

39

u/WhiteEvilBro 2d 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?

38

u/zx2167 2d 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.

1

u/Ayjayz 23h ago

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

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

8

u/Lawn_Meower_ 2d 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

6

u/ducon__lajoie 1d 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_ 1d 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_ 2d 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 1d ago edited 1d 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 1d ago

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

1

u/UntitledRedditUser 1d 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.

29

u/FACastello 2d ago

Unconst that shit

19

u/A_72_ 2d ago

const_cast is your best friend!

12

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

1

u/sb8948 1d ago

UB entered the chat

23

u/stupled 2d ago

Reinterpreter cast 🤔

6

u/throwaway58052600 2d ago

i hardly know her cast!

7

u/GegeAkutamiOfficial 1d ago

const_cast I think, I believe reinterpreter_cast fails a mutable conversion

9

u/Vesuvius079 2d ago

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

Well done.

3

u/creeper6530 1d 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 1d ago

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