<div dir="ltr">Thanks David, Halfdan for your valuable inputs.<div>Just need some more clarification so that I get the complete thing.</div><div> <div><br></div><div>Yes I understood that when we new[] an array of objects and the constructor of one of the objects throws, the already constructed objects should be destructed as the complete array construction could not get through successfully.</div>
<div><br></div><div>But assume if the constructor is not throwing, and all the objects of the array get constructed completely, than where does the need of the destructor call arise from? </div><div><br></div><div>Is it the possibility of a runtime throw which prevents the compiler from compiling a deleted destructor definition in case of an array allocation?</div>
<div><br></div><div>Also what exactly is the difference in this context when we define a default constructor in our class vs a synthesized default constructor in our class?</div><div><br></div><div>g++ seems to accept the synthesized version whereas errors when one defines a default constructor(as Halfsan mentioned above), whereas clang rejects both the versions. Who is at fault here? </div>
<div><br></div><div>Thanks,</div><div>Rahul</div><div><br></div></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Nov 14, 2013 at 12:22 AM, Halfdan Ingvarsson <span dir="ltr"><<a href="mailto:halfdan@sidefx.com" target="_blank">halfdan@sidefx.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">On 13-11-13 01:39 PM, Rahul Jain wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
But why will the destructor get called if the array is not explicitly deleted?<br>
</blockquote>
<br></div>
Because if you new[] an array of objects, and the constructor of one of the objects throws an exception, then the already constructed objects will get destructed (destroyed?).<br>
<br>
Example:<br>
#include <iostream><div class="im"><br>
<br>
int i = 0;<br>
struct A<br>
{<br></div>
A() { id = i++; if(id == 5) throw std::exception(); }<br>
~A() { std::cout << "Destroying " << id << "\n"; }<br>
<br>
int id;<br>
};<br>
<br>
int main()<br>
{<br>
try<br>
{<br>
A *a = new A[10];<br>
}<br>
catch(...) {}<br>
return 0;<br>
}<br>
<br>
- ½<br>
<br>
</blockquote></div><br></div>