mardi 5 mars 2019

unique_ptr as value in std::map

How can I initialize std::map value with structure containing unique_ptr? I have this example struct:

struct Dummy_s
{
    int a;
    std::unique_ptr<uint8_t> up;
};

below works:

std::map<uint16_t, Dummy_s> m;
m.insert(std::make_pair(0x0001, Dummy_s{1, std::make_unique<uint8_t>(42)}));
std::cout << (int)*m[0x0001].up;

but I can not figure out how to do initialization at the time of m's definition.

std::map<uint16_t, Dummy_s> m = {
// what should put here?
// my guess was:
// {0x0001, Dummy_s{1, std::make_unique<uint8_t>(42)}},
// {0x0002, Dummy_s{1, std::make_unique<uint8_t>(77)}}
};

but compiler says: call to implicitly-deleted copy constructor of std::pair.

Aucun commentaire:

Enregistrer un commentaire