在这种情况下
struct Foo {};
Foo meh() {
return std::move(Foo());
}
我非常肯定这个移动是不必要的,因为新创建的 Foo
将是一个 x 值。
但是像这样的案子呢?
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?
}
我想,这就是需要搬家的地方吧?