Let's say I have a class called Derived
, which inherits from a class called Base
. Base
is special - though it may be created using new, it has to be destructed through a custom deleter.
I would like to have the Base
in an unique_ptr
along with the custom deleter called BaseDeleter
. This also allows me to have other classes that derive from Base
assigned to it. And for the sake of exception-safety and consistency I'd like to use std::make_unique
to assing my unique_ptr
.
I created a small snippet that demonstrates what I want:
#include <memory>
class Base
{
};
class Derived : public Base
{
};
struct BaseDeleter
{
void operator()(Base* base)
{
// Perform some special deleting
}
};
class Big
{
public:
Big()
{
//pointer.reset(new Derived()); // This works!
pointer = std::make_unique<Derived, BaseDeleter>(); // But this doesn't...
}
private:
std::unique_ptr<Base, BaseDeleter> pointer;
};
int main()
{
Big clazz;
}
Unfortunately this fails to compile on both Visual Studio 2015 Update 2 and gcc-5.1. (ideone)
Why does this not work? How could one use std::make_unique
to assign such a std::unique_ptr
?