<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css" id="owaParaStyle" style="">
<!--
p
        {margin-top:0;
        margin-bottom:0}
-->
P {margin-top:0;margin-bottom:0;}</style>
</head>
<body dir="ltr" tabindex="0" fpstyle="1" aria-label="Message body">
<div name="divtagdefaultwrapper" id="divtagdefaultwrapper" style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:#000000; margin:0">
<div class="gmail_quote" style="color:rgb(40,40,40)">&gt; Note that this post from Herb arrived after&nbsp;<a href="http://www.open-std.org/pipermail/ub/2014-January/000418.html">http://www.open-std.org/pipermail/ub/2014-January/000418.html</a>&nbsp;but was sent before,
 so the thread got a little mixed up.</div>
<div><br>
</div>
<div>Yes, I've been trying to reply less on this thread until that sync'ed back up. :)</div>
<div><br>
</div>
<div>From what I've learned in this thread, the (rough) intended C&#43;&#43; model for PODs (assuming memory of the right size/alignment) would seem to be &quot;the lifetime of a B starts when you write to the memory as a B, and ends when you free the memory or write to
 the memory as different type.&quot; [Disclaimer: I'm not sure if &quot;read from the memory as a B&quot; also starts lifetime.&quot;]</div>
<div><br>
</div>
<div><span style="font-size:12pt">I think we can do better, but it seems like that's the (rough) intent of the status quo, leaving aside the question of whether the wording actually says that.</span></div>
<div><span style="font-size:12pt"><br>
</span></div>
<div>*If* that is the (rough) intent, then in:</div>
<div><br>
</div>
<div>
<blockquote class="gmail_quote" style="color:rgb(40,40,40); margin:0px 0px 0px 0.8ex; border-left-width:1px; border-left-color:rgb(204,204,204); border-left-style:solid; padding-left:1ex">
<div lang="EN-US">
<p class="MsoNormal">&nbsp; void *p = malloc(sizeof(B)); // 1</p>
<div><span style="font-size:12pt">&nbsp; B* pb = (B*)p; // 2</span></div>
</div>
</blockquote>
</div>
<blockquote class="gmail_quote" style="color:rgb(40,40,40); margin:0px 0px 0px 0.8ex; border-left-width:1px; border-left-color:rgb(204,204,204); border-left-style:solid; padding-left:1ex">
<div lang="EN-US">
<p class="MsoNormal">&nbsp; pb-&gt;i = 0; // 3</p>
<p class="MsoNormal">&nbsp; short* ps = (short*)p; // 4<br>
&nbsp; *ps = 0; // 5</p>
<p class="MsoNormal">&nbsp; free(p); // 6</p>
</div>
</blockquote>
<p><br>
</p>
<p>I assume that the reasoning would be that:</p>
<p></p>
<ul style="font-family:Calibri; font-size:12pt; margin-top:0px; margin-bottom:0px">
<li><span style="font-size:12pt">line 3 starts the lifetime of a B (we're writing to the bits of a B member, not just any int)</span></li><li>line 5 ends the lifetime of that B and begins the lifetime of a short</li><li>line 6 ends the lifetime of that short</li></ul>
<p></p>
<p><br>
</p>
<p>Again ignoring whether this is desirable, is that (roughly) the intent of the current wording?</p>
<p><br>
</p>
<p>If yes, does the wording express it (a) accurately and (b) clearly?</p>
<p><br>
</p>
<p>Finally, regardless of the above answer, do we want to change anything about the legality or semantics of the above type-punning code, such as possibly having a &quot;type-safe mode&quot; where such code is somehow not allowed unless in an &quot;extern &quot;C-compat&quot;&quot; block
 or something?</p>
<p><br>
</p>
<p>Herb</p>
<p><br>
</p>
<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 Jeffrey Yasskin &lt;jyasskin@google.com&gt;<br>
<b>Sent:</b> Friday, January 17, 2014 1:34 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">Note that this post from Herb arrived after&nbsp;<a href="http://www.open-std.org/pipermail/ub/2014-January/000418.html" title="http://www.open-std.org/pipermail/ub/2014-January/000418.html
Ctrl&#43;click or tap to follow link">http://www.open-std.org/pipermail/ub/2014-January/000418.html</a>
 but was sent before, so the thread got a little mixed up.</div>
<div class="gmail_quote"><br>
</div>
<div class="gmail_quote">On Thu, Jan 16, 2014 at 11:38 AM, Herb Sutter <span dir="ltr">
&lt;<a href="mailto:hsutter@microsoft.com" target="_blank" class="cremed">hsutter@microsoft.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex; border-left-width:1px; border-left-color:rgb(204,204,204); border-left-style:solid; padding-left:1ex">
<div lang="EN-US">
<div>
<p class="MsoNormal"><span style="font-size:11pt; font-family:Calibri,sans-serif; color:rgb(31,73,125)">Richard, it cannot mean that (or if it does, IMO we have an obvious bug) for at least two specific reasons I can think of (below), besides the general reasons
 that it would not be sensical and would violate type safety.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt; font-family:Calibri,sans-serif; color:rgb(31,73,125)"><u></u></span></p>
</div>
</div>
</blockquote>
<div><br>
</div>
<div>We do have an obvious bug in [basic.life]p1, &quot;The lifetime of an object of type T begins when storage with the proper alignment and size for type T is obtained&quot;, if we interpret &quot;obtained&quot; as &quot;obtained from the memory allocator&quot;. Even with strict uses
 of placement-new to change the type of memory, placement-new doesn't &quot;obtain&quot; any memory. If we interpret &quot;obtained&quot; as just &quot;the programmer intends a region of storage to be available for a T&quot;, as I think Richard is suggesting, the bug is only that we need
 the wording to be clearer.</div>
<div><br>
</div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex; border-left-width:1px; border-left-color:rgb(204,204,204); border-left-style:solid; padding-left:1ex">
<div lang="EN-US">
<div>
<p class="MsoNormal"><span style="font-size:11pt; font-family:Calibri,sans-serif; color:rgb(31,73,125)"><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt; font-family:Calibri,sans-serif; color:rgb(31,73,125)">First, objects must have unique addresses. Consider, still assuming B is trivially constructible:<u></u><u></u></span></p>
<p class="MsoNormal">&nbsp; void *p = malloc(sizeof(B));</p>
</div>
</div>
</blockquote>
<div><br>
</div>
<div>The lifetime of a B starts some time after-or-including the malloc() call in the above line and the access of 'pb-&gt;i' two lines down. [basic.life]p5 (&quot;Before the lifetime of an object has started ...&nbsp;The program has undefined behavior if ...&nbsp;the pointer
 is used to access a non-static data member&quot;)</div>
<div><br>
</div>
<div>The assignment to 'i' might start the lifetime of an 'int' subobject, but that's not enough to make the use of 'pb-&gt;i' defined if no 'B's lifetime has started.</div>
<div>&nbsp;</div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex; border-left-width:1px; border-left-color:rgb(204,204,204); border-left-style:solid; padding-left:1ex">
<div lang="EN-US">
<div>
<p class="MsoNormal"><u></u><u></u></p>
<p class="MsoNormal">&nbsp; B* pb = (B*)p;<br>
&nbsp; pb-&gt;i = 0;</p>
</div>
</div>
</blockquote>
<div><br>
</div>
<div>The lifetime of the B *ends* when its storage is re-used for the 'short' ([basic.life]p1 &quot;The lifetime of an object of type T ends when ... the storage which the object occupies is reused&quot;), as Daveed said. This happens some time after the access in the
 previous line, and the assignment two lines down.</div>
<div>&nbsp;</div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex; border-left-width:1px; border-left-color:rgb(204,204,204); border-left-style:solid; padding-left:1ex">
<div lang="EN-US">
<p class="MsoNormal"><u></u><u></u></p>
<p class="MsoNormal">&nbsp; short* ps = (short*)p;<br>
&nbsp; *ps = 0;<u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11pt; font-family:Calibri,sans-serif; color:rgb(31,73,125)">This cannot possibly be construed as starting the lifetime of a B object and a short object, else they would have the same address, which is illegal. Am
 I missing something?</span></p>
</div>
</blockquote>
<div><br>
</div>
<div>Both a B object and a short object have their lifetimes started in your code snippet, but the lifetimes don't overlap.&nbsp;</div>
<div><br>
</div>
<div>Confusingly, the start of these lifetimes is *not* called out in any particular line of code; it's implied by them. In particular, the casts don't have any lifetime effects (contra the straw man at
<a href="http://www.open-std.org/pipermail/ub/2014-January/000406.html">http://www.open-std.org/pipermail/ub/2014-January/000406.html</a>). The code would be just as defined (or undefined) written as:</div>
<div><br>
</div>
<div>
<p class="MsoNormal" style="font-family:arial,sans-serif; font-size:13px">&nbsp; void *p = malloc(sizeof(B));<u></u><u></u></p>
<p class="MsoNormal" style="font-family:arial,sans-serif; font-size:13px">&nbsp; B* pb = (B*)p;<br>
&nbsp; short* ps = (short*)p;<br>
&nbsp; pb-&gt;i = 0;<u></u><u></u></p>
<p class="MsoNormal" style="font-family:arial,sans-serif; font-size:13px">&nbsp; *ps = 0;</p>
<p class="MsoNormal" style="font-family:arial,sans-serif; font-size:13px"><br>
</p>
<p class="MsoNormal" style="font-family:arial,sans-serif; font-size:13px">As Matt alluded to in&nbsp;<a href="http://www.open-std.org/pipermail/ub/2014-January/000456.html">http://www.open-std.org/pipermail/ub/2014-January/000456.html</a>, it might be possible to
 say that all lifetime effects are called out in explicit expressions without breaking C compatibility, *if* we instead say that accessing the members of objects with trivial constructors can be done outside of the lifetime of such objects. I have no idea whether
 that would be better or worse than saying that lifetime effects can be implied.</p>
<p class="MsoNormal" style="font-family:arial,sans-serif; font-size:13px"><br>
</p>
<p class="MsoNormal" style="font-family:arial,sans-serif; font-size:13px">Jeffrey</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>