为什么不使用std::移动所有内容

  • 本文关键字:移动 std c++ c++11
  • 更新时间 :
  • 英文 :


我没有完全理解这个答案。为什么我们不都使用std::move类型?

示例;

std::map<int, int> try;
void foo(std::map<int, int>& try, int t1, int t2)
{
try.emplace(std::move(t1), std::move(t2));
}
int main()
{
int k = 1;
int v = 5;
try.emplace(k , v); // emplace copies
foo(try, k, v); // emplace referance
return 0;
}

那么不同的模板副本和模板引用是什么呢?我知道std::移动比使用副本更有效率。(如果我知道错了,对不起。我是初学者(那么我能用什么呢?使用副本或std::move?抱歉我英语不好。

不总是移动对象的原因是在移动对象之后,您就再也没有它了。

void f()
{
Object o;
o.stuff();
SomeFunctionTakingAReference(o);
o.stuff(); // your o object is still usable
SomeFunctionTakingAReference(std::move(o));
// Here your o object is not valid anymore. It's gone and you have a valid but different object

最新更新