何时应该在函数返回值上使用std :: move?
在这种情况下
struct Foo {};
Foo meh() {
return std::move(Foo());
}
我很确定移动是不必要的,因为新创建的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?
}
我认为需要采取行动吗?
至尊宝的传说
相关分类