我有以下这个类,我想将它作为一个shared_ptr添加到一个map中。
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> &&'
我感觉自己错过了一些明显的东西,你有什么线索吗?
make_shared
也是错误的。这个问题已经在 OP 的后续问题 中得到了修复,但为了完整起见,在这里可能值得一提。 - juanchopanza.emplace
将这对键值对插入到map中,无需手动构建它。具体可写为:map.emplace(hash32_t(image->name), std::make_shared<texture_t>())
。 - kennytm