<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, Nov 13, 2013 at 10:10 AM, Rahul Jain <span dir="ltr"><<a href="mailto:1989.rahuljain@gmail.com" target="_blank">1989.rahuljain@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><br><div>Hi all,</div><div><br></div><div><div>clang version 3.4 (194264)</div><div>Target: i386-pc-linux-gnu</div>
<div>Thread model: posix</div></div><div><br></div><div>gcc version 4.8.1<br></div><div><br>
</div><div>This is with respect to the following test code:</div><div><br></div><div><div>struct A</div><div>{</div><div> ~A() = delete;</div><div>};</div><div><br></div><div><br></div><div>int main()</div><div>{</div><div>
A* ap = new A[5];</div><div>}</div></div><div><br></div><div>While g++ compiles it without any errors , clang throws an error:</div><div><br></div><div><div>test.cpp:9:11: error: attempt to use a deleted function</div>
<div>
A* ap = new A[5];</div><div> ^</div><div>test.cpp:3:3: note: function has been explicitly marked deleted here</div><div> ~A() = delete;</div><div> ^</div><div>1 error generated.</div></div><div><br></div><div>
My question is:</div><div><br></div><div>How does the destructor come into picture when we are not explicitly deleting the memory allocated? </div><div><br></div><div>The standard says: A program that refers to a deleted function implicitly or explicitly, other than to declare it, is ill-formed.</div>
<div><br>But I suppose the destructor never gets called in this case. So ideally this test case should compile without any errors. Please correct me if my understanding is incorrect.</div><div><br></div><div><br></div><div>
Interestingly the error is not thrown when I tweak the test case to allocate a single instance instead of an array. Something like this:</div><div><br></div><div><div>struct A</div><div>{</div><div> ~A() = delete;</div>
<div>
};</div><div><br></div><div><br></div><div>int main()</div><div>{</div><div> A* ap = new A;</div><div>}</div></div><div><br></div><div>This piece of code compiles fine with both g++ and clang++ as expected.</div><div><br>
</div><div>Is this a potential bug in clang or am I missing something obvious??</div></div></blockquote><div><br></div><div>I'd have to check wording to see the finer points, but certainly the issue you are hitting is that, if A's ctor throws on one of the constructed objects in the array, the dtor for the previous array elements needs to be run.<br>
<br>I'm not sure if the language requires that this check only occur for non-nothrow ctors or not.</div></div></div></div>