向 unique_ptr 提供删除器
Supplying a deleter to a unique_ptr
C++ Primer 第 5 版(第 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
.
但是,当我尝试提供没有指针对象的删除函数时,编译器(Visual Studio 2015)抱怨(没有构造函数的实例与参数列表匹配。)。如果我给 unique_ptr
一个指针和删除器,它就可以正常工作。
那么,是我误解了什么还是这本书错了?如果这本书错了,有没有其他方法可以单独提供指向 unique_ptr
的指针和删除器?
您尝试调用的构造函数不存在。根据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()
给它指点一下以后管理。
C++ Primer 第 5 版(第 470 页)第五版中:
unique_ptr<T, D> u(d)
: Nullunique_ptr
that points to objects of typeT
that usesd
, which must be an object of typeD
in place ofdelete
.
但是,当我尝试提供没有指针对象的删除函数时,编译器(Visual Studio 2015)抱怨(没有构造函数的实例与参数列表匹配。)。如果我给 unique_ptr
一个指针和删除器,它就可以正常工作。
那么,是我误解了什么还是这本书错了?如果这本书错了,有没有其他方法可以单独提供指向 unique_ptr
的指针和删除器?
您尝试调用的构造函数不存在。根据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()
给它指点一下以后管理。