samedi 22 décembre 2018

emplace_back() vs push_back when inserting a pair into std::vector

I defined the following

std::vector<std::pair<int,int> > my_vec;
my_vec.push_back{ {1,2} } //this works
my_vec.emplace_back{ {1,2} } // this doesn't work
std::pair<int,int> temp_pair = {1,2}; 
my_vec.emplace_back{temp_pair};         //this works

I am compiling with c++11. The third line is problematic, but I thought you can use emplace_back() anywhere that you have push_back(), but this is apparently wrong. Why does the third line not work?

Aucun commentaire:

Enregistrer un commentaire