std::mutex
不可复制或移动。当你这样做时
std::mutex mtx;
auto t = std::make_pair(std::string("hello"), mtx);
and
mHeartBeatMutexes.insert(std::make_pair(std::string("hello"), mtx));
std::make_pair
尝试复制mtx
因为它是一个左值并且不能因为std::mutex
是不可复制的。
In
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 https://en.cppreference.com/w/cpp/utility/piecewise_construct过载和std::forward_as_tuple https://en.cppreference.com/w/cpp/utility/tuple/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());