mercredi 19 avril 2017

C++ map emplace

struct Foo {
    Foo(int a, int b) : a_(a), b_(b) {}
    int a_;
    int b_;
};

int main() 
{
    vector<Foo> vec;
    vec.emplace_back(1, 2);
}

The above code compiles since emplace_back constructs the Foo object in place.

int main() 
{
    map<int, Foo> m;
    m.emplace(1, 2, 3);
}

The above code does not compile. Why does emplace not construct Foo in place using args "2" and "3" ? If Foo constructor has 1 arg, the above style works. I am using gcc 4.9.2 which is not the latest. Does anyone think the above code would compile in later compilers ?

Aucun commentaire:

Enregistrer un commentaire