It'll work if you use the C++14 make_unique
or write your own one like in Yakk's answer. Basically the difference between the shared pointer behavior is that 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 a unique_pointer<Base>
it'll always use std::default_delete<Base>
as 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 constructor. As you can see the default deleter for unique_ptr
depends on the template parameter when declaring the type (unless you use make_unique
) whilst for shared_ptr
the deleter depends on the type passed to the constructor.
You can see a version that allows polymorphic delete without virtual destructor here (this version should also work in VS2012). Note that it is quite a bit hacked together and I'm currently not sure what the behavior of unique_ptr
and make_shared
in C++14 will be like, but I hope they'll make this easier. Maybe I'll look into the papers for the C++14 additions and see if something changed if I got the time later.