其他分享
首页 > 其他分享> > c – 为什么我不能用删除器创建这个unique_ptr?

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

作者:互联网

我使用SDL并遇到了一个我无法解决的问题.我想在std :: map中保留纹理(指向struct的指针),其中我使用std :: 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;
}

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

error C2338: unique_ptr constructed with null deleter pointer.

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

解决方法:

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

the_map[1];

甚至只是

int_ptr p;

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

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

the_map.emplace(1, int_ptr(new_int(), delete_int));

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

the_map.insert(std::make_pair(1, int_ptr(new_int(), delete_int)));

或者更详细但更有效率

the_map.insert(std::map<int, int_ptr>::value_type(1, int_ptr(new_int(), delete_int)));

标签:c,unique-ptr
来源: https://codeday.me/bug/20190831/1775906.html