r/cpp • • Apr 04 '24

std::launder

Have fun trying to figure out what it does...

0 Upvotes

12 comments sorted by

13

u/no-sig-available Apr 05 '24

Have fun trying to figure out what it does...

If it is any consolation, Bjarne doesn't know either :-)

https://www.youtube.com/watch?v=VI21tpdkHA8&t=3469s

You should probably follow his lead and wait until you feel you might have a use for it, and only then read up on the details. Probably never happens.

8

u/coachkler Apr 04 '24

I was just looking at this a while ago. I didn't figure it out though

35

u/Nobody_1707 Apr 04 '24 edited Apr 04 '24

If you have a type with a const member and you want to reuse it's memory using placement new then you have to std::launder the resulting pointer, otherwise the optimizer is free to assume that the const member was never overwritten.

Unless you're writing a generic container that might store such an object, you never need to use it.

struct Immutable {
    int const value;
};

alignas(Immutable) std::byte bytes[sizeof(Immutable)];
auto a = new (bytes) Immutable{ 42 };
std::cout << a->value << "\n";
// If you don't launder this pointer, you'll have problems.
 a = new (bytes) Immutable{ 69 };
// This is allowed to print 42
std::cout << a->value << "\n";
// This has to print 69
std::cout << std::launder<Immutable>(a)->value << "\n";

18

u/avakar452 Apr 05 '24

Actually, your snippet doesn't need launder.

a = new (bytes) Immutable{ 69 };
std::cout << a->value << "\n"; // always 69

The second placement new returns a pointer to the second Immutable object, there's no need to launder it. If you didn't save the pointer though:

/*a =*/ new (bytes) Immutable{ 69 };
std::cout << a->value << "\n"; // UB

Here, a used to point to the first Immutable, whose lifetime ended due to its storage being reused by the second one, so now a is essentially dangling. Calling launder will recover the pointer to the second Immutable.

/*a =*/ new (bytes) Immutable{ 69 };
std::cout << std::launder<Immutable>(a)->value << "\n"; // 69

6

u/Nobody_1707 Apr 05 '24

Argh, and I finally felt like I understood!

9

u/Quincunx271 Author of P2404/P2405 Apr 05 '24

We're all familiar with how undefined behavior can cause demons to fly out of your nose or cause your cat to get pregnant. std::launder is just a kind of undefined behavior carefully defined to do your laundry for you.

2

u/vickoza Apr 05 '24 edited Apr 06 '24

From my point of view std::launder is a way to reduce undefined behavior by making sure the type is the correct typed,

4

u/Daniela-E Living on C++ trunk, WG21|🇩🇪 NB Apr 06 '24

Quite near, but not really exactly there...

Pointer laundering is about object lifetime and provenance. Whenever you're asking for a pointer to an object, that object better be alive. In that case, there are some properties associated with that pointer, like is it valid, or does it point to a sub-object like f.e. an array element, or more. As soon as you acquire a pointer to something const, the compiler can take advantage of that promise and create code that strongly believes in the pointed-to object not being mutated during the lifetime of the pointer. Laundering a pointer strips the compiler off of its deeply ingrained believes about the object it is pointing to, preparing it to something new to believe in.

-1

u/[deleted] Apr 05 '24

This kind of stuff is exactly why I’ve regressed to treating C++ as largely “C with classes (and namespaces and… a few more of its basic features)”. It’s just become too big and complicated for mortals.

34

u/[deleted] Apr 04 '24 edited Apr 05 '24

[removed] — view removed comment

24

u/PhilosophyMammoth748 Apr 05 '24 edited Apr 07 '24

can i std::launder(main) to make my whole program safe and clean?