为什么我不能用删除器创建这个unique_ptr?

tot*_*oad 3 c++ unique-ptr

我正在使用SDL并遇到了一个我无法解决的问题.我想将纹理(指向struct的指针)保存在std::mapstd::string用作键和std::unique_ptr<texture, void(*)(texture*)>值的位置.我必须使用删除器std::unique_ptr,因为纹理必须由某个函数释放.纹理也由另一个函数创建.我将代码简化为以下内容:

#include <map>
#include <memory>

int* new_int(){ return new int; }
void delete_int(int* p){ delete p; }

typedef std::unique_ptr<int, void(*)(int*)> int_ptr;

int main()
{
    std::map<int, int_ptr> the_map;
    the_map[1] = int_ptr(new_int(), delete_int);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我尝试在Visual Studio 2012中编译此代码时,我收到以下错误:

error C2338: unique_ptr constructed with null deleter pointer.
Run Code Online (Sandbox Code Playgroud)

我觉得奇怪,因为我提供delete_int了删除指针.欢迎任何帮助,不同的方法也是如此.提前致谢!

Dan*_*rey 5

这是因为使用map::operator[]要求值类型是默认可构造的,unique_ptr在这种情况下不是.存储的指向对象的指针unique_ptr可能为空,但删除程序(如果是指针)可能不是0.您可以使用

the_map[1];
Run Code Online (Sandbox Code Playgroud)

甚至只是

int_ptr p;
Run Code Online (Sandbox Code Playgroud)

这将给你完全相同的错误.

解决方案是使用std::map::emplace:

the_map.emplace(1, int_ptr(new_int(), delete_int));
Run Code Online (Sandbox Code Playgroud)

如果由于未在您的环境中实现而无法实现,则可以使用std::map::insert:

the_map.insert(std::make_pair(1, int_ptr(new_int(), delete_int)));
Run Code Online (Sandbox Code Playgroud)

或者更详细但更有效率

the_map.insert(std::map<int, int_ptr>::value_type(1, int_ptr(new_int(), delete_int)));
Run Code Online (Sandbox Code Playgroud)