std::move 在堆栈对象上

std::move on a stack object

我知道这是一个非常基本,甚至可能令人尴尬的问题,但我无法理解它。如果我 std::move 从堆栈上的某个对象到另一个对象,当原始对象超出范围时,另一个对象是否仍然可以使用?

#include <iostream>
#include <string>

int
main(int argc, char* argv[])
{
    std::string outer_scope;

    {
        std::string inner_scope = "candy";
        outer_scope = std::move(inner_scope);
    }

    std::cout << outer_scope << std::endl;

    return 0;
}

outer_scope 在我尝试打印的地方仍然有效吗?

是的,它仍然有效,innerscope 对象失去了它之前拥有的内容的所有权,outerscope 成为所有者。 std::move 就像向量交换。如果交换外部和内部,破坏内部不会影响外部现在拥有的内容。