Why would you use std::unique_ptr ?
I had the same problem when I had to create std::map objects from std::mutex . The problem is that std::mutex is neither copyable nor movable, so I needed to build it "in place".
I could not just use emplace , because it does not work directly for default values. It is possible to use std::piecewise_construct as follows:
map.emplace(std::piecewise_construct, std::make_tuple(key), std::make_tuple());
but it is IMO complicated and less readable.
My solution is much simpler - just use operator[] - it will create a value using its default constructor and return a link to it. Or he just finds and returns a link to an existing element without creating a new one.
std::map<std::string, std::mutex> map; std::mutex& GetMutexForFile(const std::string& filename) { return map[filename];
source share