[ub] Non-virtual destructor call

Ville Voutilainen ville.voutilainen at gmail.com
Fri Dec 13 16:15:59 CET 2013


On 13 December 2013 14:56, Andrzej Krzemienski <akrzemi1 at gmail.com> wrote:
> template <typename T>
> struct Callback : std::function<void(T)>
> {
>   Callback() : std::function<void(T)> { [](T){/*ignore*/} } {}
> };
>
> I can pass it by value, convert to std::function without slicing, and it
> looks it would be safe to do this:
>
> std::function<void(T)> * fun = new Callback;
> delete fun;


Why don't you do
std::function<void(T)>*fun = new std::function<void(T)>(Callback{});
delete fun;
instead? Apply auto* where necessary.


More information about the ub mailing list