简体   繁体   中英

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM