Please explain std::launder

1,798 views
Skip to first unread message

jgot...@gmail.com

unread,
May 20, 2016, 9:28:50 PM5/20/16
to ISO C++ Standard - Discussion
  A few posts here have mentioned a function called std::launder.  I know it has something to do with pointer lifetime, but I don't really understand what.  Could someone tell me in simple terms what it does and when I might need to call it?

Thank you,

Joe Gottman

sean.mid...@gmail.com

unread,
Jun 20, 2016, 4:56:13 PM6/20/16
to ISO C++ Standard - Discussion, jgot...@gmail.com

The lifetime rules in C++ let a compiler make assumptions about the relationship between addresses and instances. Reinitializing an object with constant members (e.g. via placement new) does not interact well with those rules. std::launder is a hint to the compiler that an address can only refer to a new object and not to any object that previously existed at the same address, independent of the lifetime rules.

At least, that's what I'm gathering after a few minutes of glancing through the above links. N4430 is a little dense to read. :)

Richard Smith

unread,
Jun 21, 2016, 12:58:03 AM6/21/16
to std-dis...@isocpp.org
On Mon, Jun 20, 2016 at 1:56 PM, <sean.mid...@gmail.com> wrote:

The lifetime rules in C++ let a compiler make assumptions about the relationship between addresses and instances. Reinitializing an object with constant members (e.g. via placement new) does not interact well with those rules. std::launder is a hint to the compiler that an address can only refer to a new object and not to any object that previously existed at the same address, independent of the lifetime rules.

At least, that's what I'm gathering after a few minutes of glancing through the above links. N4430 is a little dense to read. :)

That's essentially right. From the object model perspective, it maps from a pointer to some (potentially) out-of-lifetime object at a given address to a pointer to a currently-live object at that address. From the implementation perspective, it's an optimization barrier that prevents the compiler from assuming that it knows anything about the identity of the object pointed to by the returned version of the pointer.

On Friday, May 20, 2016 at 6:28:50 PM UTC-7, jgot...@gmail.com wrote:
  A few posts here have mentioned a function called std::launder.  I know it has something to do with pointer lifetime, but I don't really understand what.  Could someone tell me in simple terms what it does and when I might need to call it?

Thank you,

Joe Gottman

--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussio...@isocpp.org.
To post to this group, send email to std-dis...@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.

Reply all
Reply to author
Forward
0 new messages