It will work if you use C ++ 14 make_unique or write your own, as in Yakka's answer. Basically the difference between common pointer behavior is what you got:
template< class T, class Deleter = std::default_delete<T> > class unique_ptr;
for unique_pointer , and as you can see, the deleter belongs to the type. If you declare unique_pointer<Base> , it will always use std::default_delete<Base> by default. But make_unique will take care of using the correct deleter for your class.
When using shared_ptr you got:
template< class Y, class Deleter > shared_ptr( Y* ptr, Deleter d );
and other overloads as a constructor. Since you can see that the default default for unique_ptr depends on the template parameter when declaring the type (unless you use make_unique ), whereas for shared_ptr deleter depends on the type passed to the constructor.
You can see the version that allows polymorphic deletion without a virtual destructor here (this version should also work in VS2012). Note that this is a bit hacky, and I'm currently not sure what the unique_ptr and make_shared behavior will look like in C ++ 14, but I hope they make it easier. Maybe Iβll look at the docs for the C ++ 14 add-ons and see if something has changed if I get the time later.
source share