无法在std :: map中使用std :: shared_ptr作为值类型?

时间:2013-08-25 20:16:43

标签: c++11 map shared-ptr

我要将以下课程添加到map作为shared_ptr

struct texture_t
{
hash32_t hash;
uint32_t width;
uint32_t height;
uint32_t handle;
};

所以我尝试使用make_pair,然后将其添加到map ...

auto texture = std::make_shared<texture_t>(new texture_t());
std::make_pair<hash32_t, std::shared_ptr<texture_t>>(hash32_t(image->name), texture);

make_pair上,我收到以下编译错误:

error C2664: 'std::make_pair' : cannot convert parameter 2 from 'std::shared_ptr<_Ty>' to 'std::shared_ptr<_Ty> &&'

我觉得我错过了一些明显的东西,任何线索?

1 个答案:

答案 0 :(得分:5)

std::make_pair不适用于显式模板参数。把它们关掉:

auto my_pair = std::make_pair(hash32_t(image->name), texture);

注意:调用make_shared也是错误的。参数传递给texture_t的构造函数,因此在这种情况下它只是:

auto texture = std::make_shared<texture_t>();
相关问题