samedi 5 décembre 2015

How I can use emplace for unordered_set that holds shared_ptr to the object?

Say I have an object:

struct Foo {
    Foo(const std::string& str1, const std::string& str1) 
        : mStr1(str1), mStr2(str2)
    {}

    std::string mStr1;
    std::string mStr2;
};

And set

typedef std::unordered_set<std::shared_ptr<Foo> , Hash, Compare> Set;

I have custom hasher and compare. But when I say:

Set set;
set.emplace(str1, str2);

I receive compile error, because the constructor of Foo is obviously not a constructor of std::shared_ptr. What I would like is when emplace needs to construct a pointer to use std::make_shared(str1, str2)

It seems that I also need a custom allocator for that, but I did not manage to implement one that satisfy the compiler.

My question is: Is what I want possible. If yes, how - is the allocator the right way to go. If yes, can you point me to an example.

Aucun commentaire:

Enregistrer un commentaire