<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Thu, Jan 16, 2014 at 10:04 AM, Gabriel Dos Reis <span dir="ltr">&lt;<a href="mailto:gdr@microsoft.com" target="_blank">gdr@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" link="blue" vlink="purple">
<div>
<p class="MsoNormal"><span style="font-family:&#39;Calisto MT&#39;,serif;color:rgb(112,48,160)">Casting a pointer from T* to U*, in and by itself, does not necessarily establish or end a lifetime.</span></p></div></div></blockquote>
<div><br></div><div>Indeed. Richard&#39;s messages make me a little uneasy about just what does begin the lifetime of a POD object, though. I&#39;m thinking of:</div><div>  struct B { int x; };  // 1</div><div>  void* p = malloc(sizeof(B)); // 2</div>
<div>  B* pb = static_cast&lt;B*&gt;(p); //3</div><div>  pb-&gt;x = 17; // 4</div><div><br></div><div>I take it as obvious that the lifetime of an object of type B has begun somewhere in this code snippet. Now the question is: which specific line begins that lifetime? As you say, casting a pointer doesn&#39;t begin or end a lifetime; I think we can rule out line 3 as the one that begins the lifetime of that B object. Line 1 doesn&#39;t look too promising either.</div>
<div><br></div><div>A literal reading of the standard ([basic.life]/1) suggests that it&#39;s line 2: </div><div>&quot;The lifetime of an object of type T begins when:</div><div>— storage with the proper alignment and size for type T is obtained, and</div>
<div>— if the object has non-trivial initialization, its initialization is complete.&quot;</div><div><br></div><div>In line 2 we have obtained storage with the proper alignment and size for type B, and the second bullet item doesn&#39;t apply since B has no non-trivial initialization. None of the other lines are relevant to the beginning of the lifetime.</div>
<div><br></div><div>But that&#39;s a little disturbing too. If we&#39;ve got:</div><div>  struct X { int x; }; // 1&#39;</div><div>  struct Y { int y; };  // 2&#39;</div><div>  void* p = malloc(sizeof(X));  // 3&#39;</div>
<div>then does line 3&#39; mean that the lifetime of some object has begun? Again a literal reading of that same standard text suggests yes. We have obtained storage with the proper alignment and size for type X, so the lifetime of an object of type X has begun. Exactly the same argument means that the lifetime of an object of type Y has begun, and similarly for every other type, mentioned or not, whose size is equal to sizeof(X).</div>
<div><br></div><div>That seems like a crazy conclusion, but I don&#39;t see how to reject it without also rejecting the idea that the lifetime of an object of type B has been established somewhere in my first code snippet. What all of this suggests to me is that the concept of object lifetime needs a little more thought for types that don&#39;t have initialization.</div>
<div><br></div><div>                         --Matt</div></div></div></div>