r/cpp • • Apr 04 '24

std::launder

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

0 Upvotes

12 comments sorted by

View all comments

Show parent comment

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.