2016-11-18 41 views
1

我讀尼古拉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是一個局部變量,但轉移到返回。

然而,如方案quited由水平calltree水平,棧存儲器將釋放。當調用堆棧解除時這會成爲問題嗎?

注意:這個問題不同於c++11 Return value optimization or move?:這個問題是關於move是有潛在危險的,而另一個問題是關於是否主動禁止複製elision或讓編譯器決定。

回答

2

Unless otherwise specified, all standard library objects that have been moved from are placed in a valid but unspecified state.有效意味着它可以被安全銷燬(例如在堆棧放卷時)。在您的例子s沒有返回,但存儲在希望,但是如果它是在正常的方式返回return s;編譯器可以隱式調用return move(s);

+0

我想'move'手段「竊取存儲位置」,因此沒有必要複製?這是一個STL特定的實現嗎? – athos

+1

'move'頗具意味「偷內存的位置,如果它是有用的或任何你想要的,但留在有效的狀態這個對象」。移動字符串後,內部數據指針可以爲空,如果析構函數不嘗試釋放空指針,則該指針有效。 –

2

這是沒有問題的。

移動語義移動變量的內部值而不是變量本身。所以你仍然有一個單獨的目標字符串到源字符串。

整個練習的效果表面上像一個副本除了事後源對象有其值改爲一個未知數。

它仍然是有效的對象(的未知值),並且將當堆棧退繞適當銷燬。

+0

我以爲'移動'的意思是「盜取內存位置」,所以不需要複製? – athos

+1

@athos Well * move *是關於竊取可能或可能不等於內存位置的內部值。從外部看,它只是看起來(和表現)像一個副本,除了被移動的對象已經(可能)改變了它的值。 – Galik

相關問題