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

Col*_*ett 3 map shared-ptr c++11

我有以下课程,我想添加到mapa shared_ptr.

struct texture_t
{
hash32_t hash;
uint32_t width;
uint32_t height;
uint32_t handle;
};
Run Code Online (Sandbox Code Playgroud)

所以我尝试使用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);
Run Code Online (Sandbox Code Playgroud)

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

error C2664: 'std::make_pair' : cannot convert parameter 2 from 'std::shared_ptr<_Ty>' to 'std::shared_ptr<_Ty> &&'
Run Code Online (Sandbox Code Playgroud)

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

Vau*_*ato 5

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

auto my_pair = std::make_pair(hash32_t(image->name), texture);
Run Code Online (Sandbox Code Playgroud)

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

auto texture = std::make_shared<texture_t>();
Run Code Online (Sandbox Code Playgroud)

  • 您可以使用`.emplace`将对插入地图而无需手动构建它.`map.emplace(hash32_t(image-> name),std :: make_shared <texture_t>())`. (3认同)