<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Fri, Nov 25, 2016 at 11:23 PM, Mehdi Amini <span dir="ltr"><<a href="mailto:mehdi.amini@apple.com" target="_blank">mehdi.amini@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word">You don’t need the main and the union to reproduce, extracting foo() alone in its single own file is enough:<div><br></div><div><div>void *operator new(decltype(sizeof 0), void *) noexcept;</div><div>float *qq;</div><div>void foo(int *p, int *q, long unk) {</div><div>   for (long i = 0; i < unk; ++i) {</div><div>      ++*p;</div><div>      qq = new (static_cast<void *>(&q[i])) float(42);</div><div>   }</div><div>}</div></div><div><br></div><div>LICM will get the store to p out of the loop, conceptually turning it into:</div><div><div><br></div><div>void foo(int *p, int *q, long unk) {</div><div>   for (long i = 0; i < unk; ++i) {</div><div>      qq = new (static_cast<void *>(&q[i])) float(42);</div><div>   }</div><div>   ++*p;</div><div>}</div></div><div><br></div><div><br></div><div>Now I don’t know if the use of placement new in this example is legal in the first place. I thought calling delete before using placement new was mandatory.</div></div></blockquote><div>There's wording describing the reuse of storage and the situation of neither calling the destructor nor using a <i>delete-expression</i>.<br><br>N4604 subclause 3.8 [basic.life] paragraph 5:<br><br><div style="margin-left:40px">For an object of a class type with a non-trivial destructor, the program is not required to call the destructor explicitly before the storage which the object occupies is reused or released; however, if there is no explicit call to the destructor or if a <i>delete-expression</i> is not used to release the storage, the destructor shall not be implicitly called and any program that depends on the side effects produced by the destructor has undefined behavior.<br></div><br></div><div>I read the above wording as saying that, even if the storage is currently occupied by an object of class type with a non-trivial destructor, the storage can be "simply" reused (without undefined behaviour). The lifetime of the previous object simply ends when the placement new occurs (more wording below).<br><br>3.8 [basic.life] paragraph 1:<br><br><div style="margin-left:40px">The lifetime of an object <i>o</i> of type <span style="font-family:monospace,monospace">T</span> ends when [ ... ] the storage which the object occupies is released, or is reused by an object that is not nested within <i>o</i>.<br></div><br></div></div></div></div>