Why does this insert create a copy?

Viewed 130

I was watching this video on C++ to learn more, and I didn't quite get this explanation here (exact timestamp in the aforementioned video).

Code was

void Benchmark_Slow(int iters){
    std::unordered_map<string, int> m;
    std::pair<const string, int> p = {};
    while (iters--) m.insert(p);
//                           ^
// Why does this create a copy?
}

He says that the && overload called, instead of the const & overload, thus creating a copy. My questions in trying to understand this are,

  1. Why does it create a copy? Is it because the argument is not an r-value reference (has a name), and thus cannot be 'constructed-in-place'?
  2. Does m.insert(std::move(p)) fix this?
0 Answers
Related