jeudi 26 mai 2016

Map moving emplace with lvalue

I am not sure if I understand correctly this code:

template<typename M>
void add(M& m,int i, std::string s)
{
  m.emplace(i,std::move(s));
}

int main()
{
  std::map<int,std::string> m;
  add(m,1,"foo");
}

When add is called, the std::string and the int are copied. The method emplace construct a std::pair which is moved into the std::map (no copy needed ). But the copy of int is a lvalue while the copy of std::string is cast to a rvalue, therefore which constructor is called to construct the std::pair? Since one argument cannot be moved, I suppose that an additional copy take place here. Is it right? Obviously, if I cast also the copy of int to a rvalue, I would expect no additional copies.

Aucun commentaire:

Enregistrer un commentaire