<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:"Calisto MT";
        panose-1:2 4 6 3 5 5 5 3 3 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-reply;
        font-family:"Calisto MT","serif";
        color:#7030A0;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri","sans-serif";}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-family:&quot;Calisto MT&quot;,&quot;serif&quot;;color:#7030A0">No, we would have exception for non-static data member access for unconstructed objects – this would be conceptually the same form of exception we grant in constructors for vcalls
 or typeids and even certain member access.<o:p></o:p></span></p>
<p class="MsoNormal"><a name="_MailEndCompose"><span style="font-family:&quot;Calisto MT&quot;,&quot;serif&quot;;color:#7030A0"><o:p>&nbsp;</o:p></span></a></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt">
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;">From:</span></b><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;"> ub-bounces@open-std.org [mailto:ub-bounces@open-std.org]
<b>On Behalf Of </b>Richard Smith<br>
<b>Sent:</b> Friday, January 17, 2014 3:43 PM<br>
<b>To:</b> WG21 UB study group<br>
<b>Subject:</b> Re: [ub] type punning through congruent base class?<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<div>
<div>
<div>
<p class="MsoNormal">On 17 January 2014 15:19, Gabriel Dos Reis &lt;<a href="mailto:gdr@microsoft.com" target="_blank">gdr@microsoft.com</a>&gt; wrote:<o:p></o:p></p>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in">
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt">| From what I've learned in this thread, the (rough) intended C&#43;&#43; model for<br>
| PODs (assuming memory of the right size/alignment) would seem to be &quot;the<br>
| lifetime of a B starts when you write to the memory as a B, and ends when you<br>
| free the memory or write to the memory as different type.&quot; [Disclaimer: I'm<br>
| not sure if &quot;read from the memory as a B&quot; also starts lifetime.&quot;]<o:p></o:p></p>
</div>
<p class="MsoNormal">that would be close to what C does with effective types.<o:p></o:p></p>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt"><br>
| I think we can do better,<o:p></o:p></p>
</div>
<p class="MsoNormal">Same here.<o:p></o:p></p>
<div>
<p class="MsoNormal"><br>
| but it seems like that's the (rough) intent of the<br>
| status quo, leaving aside the question of whether the wording actually says<br>
| that.<br>
|<br>
|<br>
| *If* that is the (rough) intent, then in:<br>
|<br>
|<br>
| &nbsp; &nbsp; &nbsp; &nbsp; void *p = malloc(sizeof(B)); // 1<br>
|<br>
| &nbsp; &nbsp; &nbsp; &nbsp; B* pb = (B*)p; // 2<br>
|<br>
| &nbsp; &nbsp; &nbsp; &nbsp; pb-&gt;i = 0; // 3<br>
|<br>
| &nbsp; &nbsp; &nbsp; &nbsp; short* ps = (short*)p; // 4<br>
| &nbsp; &nbsp; &nbsp; &nbsp; *ps = 0; // 5<br>
|<br>
| &nbsp; &nbsp; &nbsp; &nbsp; free(p); // 6<br>
|<br>
|<br>
|<br>
|<br>
| I assume that the reasoning would be that:<br>
|<o:p></o:p></p>
</div>
<p class="MsoNormal">| * &nbsp; &nbsp; line 3 starts the lifetime of a B (we're writing to the bits of a B member,<br>
| not just any int)<br>
<br>
line 3 does not write as a B, so it wouldn't count -- just like in C.<o:p></o:p></p>
</blockquote>
<div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<div>
<p class="MsoNormal">Would that not give 'pb-&gt;i = 0' undefined behavior, because 'pb' does not, in fact, point to a B object?<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<div>
<p class="MsoNormal">The only clear governing rule I can find is 3.8/5, but that only applies if the storage ever actually contains a B object. Consider:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<div>
<p class="MsoNormal">&nbsp; void *p = malloc(sizeof(B));<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">&nbsp; B *pb = (B*)p;<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">&nbsp; pb-&gt;i = 0;<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">&nbsp; // ...<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">&nbsp; new (p) B;<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<div>
<p class="MsoNormal">In this example, &quot;pb-&gt;i = 0&quot; has undefined behavior, because we performed member access on a pointer that points to storage where a B object will later be constructed. It seems strange that the definedness of 'pb-&gt;i = 0' would depend on
 how the storage is used later.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in">
<p class="MsoNormal">| * &nbsp; &nbsp; line 5 ends the lifetime of that B and begins the lifetime of a short<br>
| * &nbsp; &nbsp; line 6 ends the lifetime of that short<br>
<br>
Agreed.<o:p></o:p></p>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt"><br>
| Again ignoring whether this is desirable, is that (roughly) the intent of the<br>
| current wording?<o:p></o:p></p>
</div>
<p class="MsoNormal">Almost: we never started the lifetime of a B object.<o:p></o:p></p>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt"><br>
| If yes, does the wording express it (a) accurately and (b) clearly?<o:p></o:p></p>
</div>
<p class="MsoNormal">My understanding is &quot;no&quot; for both.<o:p></o:p></p>
</blockquote>
<div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<div>
<p class="MsoNormal">Hear, hear =)<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">&nbsp;<o:p></o:p></p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in">
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt">| Finally, regardless of the above answer, do we want to change anything about<br>
| the legality or semantics of the above type-punning code, such as possibly<br>
| having a &quot;type-safe mode&quot; where such code is somehow not allowed unless in<br>
| an &quot;extern &quot;C-compat&quot;&quot; block or something?<o:p></o:p></p>
</div>
<p class="MsoNormal">This is a good question! &nbsp;Which definitely needs a paper :-)<br>
<br>
My view is that if the storage isn't declared or accessed as a B, then it isn't a B .<o:p></o:p></p>
</blockquote>
<div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<div>
<p class="MsoNormal">It seems you don't consider member access as a B to qualify here. Is that your intent?<o:p></o:p></p>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>