On Thu, May 30, 2013 at 4:45 PM, Ville Voutilainen <span dir="ltr"><<a href="mailto:ville.voutilainen@gmail.com" target="_blank">ville.voutilainen@gmail.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr"><br><div class="gmail_extra">If you just do an opt.emplace(n2); followed by opt->ref = 1; you never formed a pointer/reference to the original<br>object</div></div></blockquote><div><br>Why? I presume that the intention behind 3.8/7 was to allow optimizations described below:<br>
<br><span style="font-family:courier new,monospace"> #include <iostream><br><br> struct X<br> {<br> int &ref;<br> };<br><br> void f(X &);<br><br> int main()<br> {<br> int n = 0;<br>
X x{n};<br> f(x);<br> x.ref = 5;<br> std::cout << n << std::endl;<br> }</span><br><br>Here a compiler is allowed to assume that<br><br><span style="font-family:courier new,monospace"> x.ref = 5</span><br>
<br>is equivalent to<br><br><span style="font-family:courier new,monospace"> n = 5;</span><br><br>and therefore<br><br><span style="font-family:courier new,monospace"> std::cout << n << std::endl;</span><br>
<br>is equivalent to<br><br><span style="font-family:courier new,monospace"> std::cout << 5 << std::endl;</span><br><br>regardless of the definition of f (which may be unknown for compiler). There is no legal way to modify reference <span style="font-family:courier new,monospace">x.ref</span> after its initialization so that it would refer to a different location. Even if we overwrite the storage of x by construction of a new object of type X at address &x (our f could do such thing), a compiler may assume that <span style="font-family:courier new,monospace">x.ref</span> is untouched. The same applies to the original example with optional.<br>
</div></div><br>