Zylon D. Lite
Zylon D. Lite

Reputation: 97

Supplying a deleter to a unique_ptr

In the fifth edition of C++ Primer 5th Edition(p. 470):

unique_ptr<T, D> u(d) : Null unique_ptr that points to objects of type T that uses d, which must be an object of type D in place of delete.

However, when I tried to supply the deleter function without a pointer object, the compiler(Visual Studio 2015) complains( no instance of constructor matches the argument list.). If I give the unique_ptr a pointer along with the deleter, it works fine. So, am I misinterpreting something or is the book just wrong? If the book is wrong, is there any other way I can seperately supply a pointer and deleter to a unique_ptr?

Upvotes: 0

Views: 135

Answers (1)

Andy
Andy

Reputation: 30418

The constructor you are trying to call doesn't exist. According to MSDN, the only constructors that take a deleter function take a pointer as well. If you want to initialize a unique_ptr with a deleter but don't want to give it a value yet, you could always pass nullptr as the first parameter and call unique_ptr::reset() to give it a pointer to manage later.

Upvotes: 1

Related Questions