为什么我不能使用删除器创建此unique_ptr

Why can I not create this unique_ptr with deleter?

本文关键字:创建 unique ptr 删除 不能 为什么      更新时间:2023-10-16

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