<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<style type="text/css" id="owaParaStyle" style="display: none;"><!--P {margin-top:0;margin-bottom:0;}--></style>
</head>
<body dir="ltr" fpstyle="1" aria-label="Message body" tabindex="0" id="">
<div name="divtagdefaultwrapper" id="divtagdefaultwrapper" style="font-family: Calibri,Arial,Helvetica,sans-serif; font-size: 12pt; color: #000000; margin: 0">
<p>Richard, I'm not sure I understand your position... Given the following complete program ...</p>
<p><br>
</p>
<div style="color: rgb(40, 40, 40);">&nbsp; struct B { int i; };</div>
<div style="color: rgb(40, 40, 40);">&nbsp; struct D : B {<span style="font-size: 12pt;">&nbsp;};</span></div>
<div style="color: rgb(40, 40, 40);"><br>
</div>
<div style="color: rgb(40, 40, 40);">&nbsp; int main() {</div>
<div style="color: rgb(40, 40, 40);">&nbsp; &nbsp; B b; // line X</div>
<div style="color: rgb(40, 40, 40);">&nbsp; }</div>
<div><br>
</div>
<div>... a<span style="font-size: 12pt;">re you actually saying that</span><span style="font-size: 12pt;">&nbsp;</span><span style="font-size: 12pt;">line X starts the lifetime of an object of type D? or just setting up a strawman? (Sorry, I really couldn't tell.)</span></div>
<div><br>
</div>
<div>If yes, then given the following complete program ...</div>
<div><br>
</div>
<div>
<div style="color: rgb(40, 40, 40);">&nbsp; struct C { long long i; };</div>
<div style="color: rgb(40, 40, 40);"><br>
</div>
<div style="color: rgb(40, 40, 40);">
<div>&nbsp; int main() {</div>
<div>&nbsp; &nbsp; C c; // line Y</div>
<div>&nbsp; }</div>
<div><br>
</div>
</div>
</div>
<div>... are you saying that line Y could start the lifetime of an object of type D (which is not mentioned in the code), double, shared_ptr&lt;widget&gt;, or any other type than C, as long as the size of that other type is the same or less than sizeof(C)?</div>
<div><br>
</div>
<div>Herb</div>
<div><br>
</div>
<p><br>
</p>
<div style="color: rgb(40, 40, 40);">
<hr tabindex="-1" style="display:inline-block; width:98%">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" color="#000000" style="font-size:11pt"><b>From:</b> ub-bounces@open-std.org &lt;ub-bounces@open-std.org&gt; on behalf of Richard Smith &lt;richardsmith@google.com&gt;<br>
<b>Sent:</b> Monday, January 6, 2014 3:44 PM<br>
<b>To:</b> WG21 UB study group<br>
<b>Subject:</b> Re: [ub] type punning through congruent base class?</font>
<div>&nbsp;</div>
</div>
<div>
<div dir="ltr">
<div class="gmail_extra">
<div class="gmail_quote">On Mon, Jan 6, 2014 at 10:22 AM, Jason Merrill <span dir="ltr">
&lt;<a href="mailto:jason@redhat.com" target="_blank">jason@redhat.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex; border-left:1px #ccc solid; padding-left:1ex">
<div class="im">On 01/06/2014 04:26 AM, Fabio Fracassi wrote:<br>
&gt; if it is not (legal): could we make it legal or would we run afoul of<br>
&gt; the aliasing rules?<br>
<br>
</div>
The access is not allowed by the aliasing rules in 3.10. &nbsp;But it seems<br>
that this would be:<br>
<div class="im"><br>
struct B {<br>
&nbsp; &nbsp;int i;<br>
};<br>
<br>
struct D {<br>
</div>
&nbsp; &nbsp;B bmem;<br>
&nbsp; &nbsp;void foo() { /* access bmem.i */ }<br>
};<br>
<br>
B b;<br>
reinterpret_cast&lt;D&amp;&gt;(b).foo();<br>
<br>
because B is a non-static data member of D, and 9.2/19 guarantees that<br>
the address of D::bmem is the same as the address of the D object.</blockquote>
<div><br>
</div>
<div>How is that fundamentally different? 9.3.1/2 makes that UB too, if 'reinterpret_cast&lt;D&amp;&gt;(b)' does not refer to an object of type 'b'. And within D::foo, the implicit this-&gt;bmem would have the same problem.</div>
<div><br>
</div>
<div><br>
</div>
<div>If I might play devil's advocate for a moment...</div>
<div><br>
</div>
<div>&nbsp; struct B { int i; };</div>
<div>&nbsp; struct D : B {</div>
<div>&nbsp; &nbsp; void foo();</div>
<div>&nbsp; };</div>
<div><br>
</div>
<div>&nbsp; B b;</div>
<div><br>
</div>
<div>I claim this line starts the lifetime of an object of type D. Per [basic.life]p1, the lifetime of an object of type 'D' begins when storage with the proper alignment and size for type T is obtained (which &quot;B b&quot; happens to satisfy). The object does not
 have non-trivial initialization, so the second bullet does not apply.</div>
<div><br>
</div>
<div>(This is the same argument that makes this valid:</div>
<div><br>
</div>
<div>&nbsp; D *p = (D*)malloc(sizeof(D));</div>
<div>&nbsp; p-&gt;foo();</div>
<div><br>
</div>
<div>... so any counterargument will need to explain why the two cases are fundamentally different.)</div>
<div><br>
</div>
<div>Then:</div>
<div><br>
</div>
<div>&nbsp; reinterpret_cast&lt;D&amp;&gt;(b).foo();</div>
<div><br>
</div>
<div>... is valid, because the cast produces the same memory address, and that memory address contains an object of type 'D' (as claimed above).</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>