相关疑难解决方法(0)

何时应该在函数返回值上使用std :: move?

在这种情况下

struct Foo {};
Foo meh() {
  return std::move(Foo());
}
Run Code Online (Sandbox Code Playgroud)

我很确定移动是不必要的,因为新创建的Foo将是一个xvalue.

但在这种情况下呢?

struct Foo {};
Foo meh() {
  Foo foo;
  //do something, but knowing that foo can safely be disposed of
  //but does the compiler necessarily know it?
  //we may have references/pointers to foo. how could the compiler know?
  return std::move(foo); //so here the move is needed, right?
}
Run Code Online (Sandbox Code Playgroud)

我认为需要采取行动吗?

c++ move-semantics c++11

109
推荐指数
4
解决办法
5万
查看次数

标签 统计

c++ ×1

c++11 ×1

move-semantics ×1