<div dir="ltr"><div><div><div><div><div><div><div><div><div>Hi Everybody,<br></div><div><br>The standard says (Sect 5.3.5, para 3) that &quot;In the first alternative (<i>delete object</i>), if the static type of the object to be deleted is different from its dynamic type, the static type shall be a base class of the dynamic type of the object to be deleted and the static type shall have a virtual destructor or the behavior is undefined.&quot;<br>
<br></div>This requirement is sound in general, but it prevents certain usages that although uncommon appear to be valid and could be allowed.<br><br></div>The usage I have in mind is what Mathew Wilson calls &quot;veneers&quot; (see <a href="http://synesis.com.au/resources/articles/cpp/veneers.pdf">http://synesis.com.au/resources/articles/cpp/veneers.pdf</a>). In short, if I do not like that std::function when default constructed is something that throws upon the call to operator(), I extend it as follows:<br>
<br></div><div>template &lt;typename T&gt;<br></div>struct Callback : std::function&lt;void(T)&gt;<br>{<br>  Callback() : std::function&lt;void(T)&gt; { [](T){/*ignore*/} } {}<br>};<br><br></div>I can pass it by value, convert to std::function without slicing, and it looks it would be safe to do this:<br>
<br>std::function&lt;void(T)&gt; * fun = new Callback;<br></div>delete fun;<br><br></div>There is no harm, except that this is defined as UB. Could the rule be relaxed to say that if the derived type is layout-compatible with the base class and its destructor is implicitly declared or explicitly defaulted, the behaviur is well defined?<br>
<br></div>Or is there a good reason for this restriction?<br><br></div>Regards,<br></div>&amp;rzej<br></div>