所以我有一个std::unordered_map<std::string, std::shared_ptr<T>>,我想try_emplace用来添加/引用元素.以前,它只是一个std::unordered_map<std::string, T>并且T有一个构造函数只需要一个std::string,所以我用它myMap.try_emplace(myString, myString).first->second来引用T并在必要时创建它.
但是,如果我只是将其更改为myMap.try_emplace(myString, std::make_shared<T>(myString)).first->second,它当然会T每次构建它.
什么是解决这个问题最惯用的方式?为了澄清,我想使用共享指针在堆上构造对象,并且当且仅当映射中不存在匹配元素时,才将共享指针插入到无序映射中.
c++ ×1