通过移动 return 局部变量可以吗?

is it ok to return a local variable by move?

我正在阅读 Nicolai M. Josuttis 的 2nd edition of "The C++ Standard Library" covering C++11,其中第 18 章:并发,第 969 页和第 970 页给出了示例程序:

// concurrency/promise1.cpp
#include <thread>
#include <future>
#include <iostream>
#include <string>
#include <exception>
#include <stdexcept>
#include <functional>
#include <utility>
void doSomething (std::promise<std::string>& p)
{
    try {
        // read character and throw exceptiopn if ’x’
        std::cout << "read char (’x’ for exception): ";
        char c = std::cin.get();
        if (c == ’x’) {
            throw std::runtime_error(std::string("char ")+c+" read");
        }
        ...
        std::string s = std::string("char ") + c + " processed";
        p.set_value(std::move(s)); // store result
    }
    catch (...) {
        p.set_exception(std::current_exception()); // store exception
    }
}

int main()
{
    try {
        // start thread using a promise to store the outcome
        std::promise<std::string> p;
        std::thread t(doSomething,std::ref(p));
        t.detach();
        ...
        // create a future to process the outcome
        std::future<std::string> f(p.get_future());
        // process the outcome
        std::cout << "result: " << f.get() << std::endl;
    }
    catch (const std::exception& e) {
        std::cerr << "EXCEPTION: " << e.what() << std::endl;
    }
    catch (...) {
        std::cerr << "EXCEPTION " << std::endl;
    }
}

这里strings是局部变量但是移到了return.

但是随着程序逐级退出调用树,栈内存也会被释放。当调用堆栈展开时,这会成为问题吗?

注意:这个问题和c++11 Return value optimization or move?不同:这个问题是关于move is potentially dangerous,而另一个问题是关于是否主动禁止复制省略或让编译器决定。

Unless otherwise specified, all standard library objects that have been moved from are placed in a valid but unspecified state. 有效意味着它可以安全销毁(例如在堆栈展开时)。在您的示例中 s 未返回但存储在 promise 中,但如果以正常方式返回 return s; 编译器可以隐式调用 return move(s);.

这不是问题。

移动语义移动变量的内部值,而不是变量本身。所以你仍然有一个单独的目标字符串到源字符串。

整个练习表面上看起来像 复制 除了之后源对象将其值更改为未知数量。

它仍然是一个有效的对象(未知值),当堆栈展开时将被正确销毁。