mercredi 2 août 2017

Correct usage of emplace when value type is a struct

#include <iostream>
#include <map>
using namespace std;

struct FooStruct 
{
    int a;
    int b;
};

int main()
{
    map<int, FooStruct> fooMap;
    fooMap.emplace<int, FooStruct>(0, {1, 2});
    return 0;
}

Is the above a correct usage of emplace? Is the above form better than

fooMap.emplace(make_pair<int, FooStruct>(0, {1, 2}));

Or are these forms equivalent and both of them avoid creating a temporary copy of FooStruct?

Aucun commentaire:

Enregistrer un commentaire