vendredi 24 novembre 2017

C++ map brace initialization and unique ptr

Why can't I use map's braces initialization with unique_ptr?

I can create and insert into unordered map using make_pair and the [] operator.

std::unordered_map<std::string, std::unique_ptr<A>> map;

map.insert(std::make_pair("hello",std::make_unique<A>()));
map["foo"] = std::make_unique<A>();

But I can't figure out why it fails when using braces.

map.insert({"foo", std::make_unique<A>()}); // Error

error: use of deleted function ‘std::pair<_T1, _T2>::pair(const std::pair<_T1, _T2>&)

Aucun commentaire:

Enregistrer un commentaire