std::mutex
不可复制或移动。当你这么做的时候
std::mutex mtx;
auto t = std::make_pair(std::string("hello"), mtx);
mHeartBeatMutexes.insert(std::make_pair(std::string("hello"), mtx));
std::make_pair
mtx
不可复制。
在
std::mutex mtx;
auto t = std::make_pair(std::string("hello"), 1);
1
是一个整数文本,它具体化为一个临时的整数,被移动(复制,因为它是同一件事),这一切都好。
std::unordered_map<std::string, std::mutex>
您需要做的是使用emplace函数在
unordered_map
std::piecewise_construct
过载和
std::forward_as_tuple
std::unordered_map<std::string, std::mutex> foo;
foo.emplace(std::piecewise_construct,
std::forward_as_tuple("hello"),
std::forward_as_tuple());