<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""></div><div class=""><div><blockquote type="cite" class=""><div class="">On 2016–01–10, at 5:42 AM, Gabriel Dos Reis &lt;<a href="mailto:gdr@microsoft.com" class="">gdr@microsoft.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="WordSection1" style="page: WordSection1; font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif;" class=""><span style="font-size: 11pt; font-family: Calibri, sans-serif; color: rgb(31, 73, 125);" class="">My assumption has always been that std::launder is a zero-overhead abstraction (really a no-op at the machine level) whole sole purpose is to inform the static semantics elaborator about the lifetime and type of a given storage.&nbsp; I hope we aren’t considering anything more complicated than that.</span></div></div></div></blockquote><div><br class=""></div><div>This perfectly expresses my expectation as well. But now it looks slightly different: The type remains malleable, and rather than marking an area of storage, <font face="Courier" class="">launder</font> provides a single blessed reference to an unbounded region. (If&nbsp;<font face="Courier" class="">reinterpret_cast</font>&nbsp;and downcasts are valid on the result of&nbsp;<font face="Courier" class="">launder</font>, what’s its connection to the type system? Why can’t it operate on&nbsp;<font face="Courier" class="">void*</font>?)</div><div><br class=""></div><div>And, it’s not exactly zero-overhead, as its purpose is to disable an optimization which might be harmless. For example, take&nbsp;<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4303.html" class="">N4303</a>’s motivating case in <font face="Courier" class="">std::optional</font>. Statically,&nbsp;<font face="Courier" class="">optional</font>’s contained object might have been created by placement-new, so the accessors always need to launder their return value. This would seem to make <font face="Courier" class="">optional</font> slower than “native” objects.</div><div><br class=""></div><div><font face="Courier" class="">namespace std {</font></div><div><span style="font-family: Courier;" class="">template&lt; typename t &gt;</span></div><div><font face="Courier" class="">t &amp; optional&lt; t &gt;::operator * ()</font></div><div><font face="Courier" class="">&nbsp; &nbsp; { return * launder( &amp; this-&gt;storage.object ); }</font></div><div><font face="Courier" class="">}</font></div><div><font face="Courier" class=""><br class=""></font></div><div><font face="Courier" class="">void foo() {</font></div><div><font face="Courier" class="">&nbsp; &nbsp; int i;</font></div><div><font face="Courier" class="">&nbsp; &nbsp; std::optional&lt; int &gt; o = 3;</font></div><div><font face="Courier" class=""><br class=""></font></div><div><font face="Courier" class="">&nbsp; &nbsp; i = *o; // Must load *o from memory because address was laundered.</font></div><div><font face="Courier" class="">&nbsp; &nbsp; i = *o + *o; // Reload&nbsp;twice more. Will optimizers&nbsp;second-guess launder?</font></div><div><font face="Courier" class="">}</font></div><div><br class=""></div><div>Likewise, MSVC appears to be remembering the value inside my wrapper’s NSDM embedded storage. It’s a beneficial optimization to the common case of a sequence of accesses, so it would be nice not to lose it.</div><div><br class=""></div><div>It would also be easier-to-use if the assignment operator could inform the compiler when a lifetime begins, as opposed to the accessors providing notification that a new lifetime might have already begun. But, I suppose that’s not feasible, or we wouldn’t need <font face="Courier" class="">launder</font> in the first place.</div></div></div><div class=""><br class=""></div></body></html>