<div dir="ltr"><div><div><div><div><div><div><div><div><div><div><div>Hi,<br><br></div>This is a gcc test case<br><br><i>extern "C" int printf (const char *, ...);<br><br>int c;<br>int r;<br><br>struct A<br>{<br>
A() { printf ("A()\n"); if (c++) r = 1; }<br> A(const A&) { printf ("A(const A&)\n"); ++c; }<br> ~A() { printf ("~A()\n"); --c; }<br>};<br><br>struct B<br>{<br> B(int, const A& = A()) { printf ("B()\n"); }<br>
};<br><br>int main()<br>{<br> B b[] = { 0, 1 };<br> return r;<br>}</i><br><br></div>Output from Clang (latest trunk):<br><br><i>A()<br>B()<br>A()<br>B()<br>~A()<br>~A()</i><br><br></div>Output from gcc 4.8 :<br><br><i>A()<br>
B()<br>~A()<br>A()<br>B()<br>~A() </i><br><br></div>As we can see, the destructor of temporary objects are not called immediately in case of clang.<br>The temporary A passed to the constructor for b[0] should have been destroyed before going on to construct b[1]. <br>
<br></div>According to standards,<br></div><br clear="all">The temporary to which the reference is bound or the temporary that is the complete object of a subobject to which the reference is bound persists for the lifetime of the reference except:<br>
— A temporary bound to a reference member in a constructor’s ctor-initializer ( 12.6.2) persists until the constructor exits.<br>— A temporary bound to a reference parameter in a function call ( 5.2.2) persists until the completion of the full-expression containing the call.<br>
<br></div>So even though A's temporary is bound to reference, it should get destroyed as soon as b[0] gets constructed before going forward to construct b[1].<br><br></div>This seems similar to <a href="http://llvm.org/bugs/show_bug.cgi?id=16476">http://llvm.org/bugs/show_bug.cgi?id=16476</a><br>
<br></div>Richard, any comment/help on this from you will be most welcomed (Since bug 16476 was filed by you :)). Does this require major change in code and handling of temporaries?<br><br></div>Is this also related to <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1634">http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1634</a> ?<br>
<br><br></div><div>-- <br><div><div><div><div><div><div><div><div><div><div><div><div><div><div>With regards,<br>Suyog Sarda<br>
</div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div>