2013-04-25 52 views
0

我試圖編譯這個(有一些小的明顯的修復) c++11 async continuations or attempt at .then() semantics 使用clang(最新版本)與libC++,它不會編譯:沒有匹配函數調用'然後」。C++ 11異步+然後

我找不到原因...你能幫我嗎?

+2

是否包含未來標題? – ForEveR 2013-04-25 06:50:14

+1

無論如何,如果你知道語言 - 你應該看到一個錯誤 - 那麼應該是'auto then(F f,W w) - > std :: future ForEveR 2013-04-25 06:58:48

回答

1

答案在少數地方缺少move。如果沒有這些動作,future正在被要求複製,但它不能,因爲它是一種移動類型。

#include <future> 

namespace detail 
{ 

template<typename F, typename W, typename R> 
struct helper 
{ 
    F f; 
    W w; 

    helper(F f, W w) 
     : f(std::move(f)) 
     , w(std::move(w)) 
    { 
    } 

    helper(const helper& other) 
     : f(other.f) 
     , w(other.w) 
    { 
    } 

    helper(helper&& other) 
     : f(std::move(other.f)) 
     , w(std::move(other.w)) 
    { 
    } 

    helper& operator=(helper other) 
    { 
     f = std::move(other.f); 
     w = std::move(other.w); 
     return *this; 
    } 

    R operator()() 
    { 
     f.wait(); 
     return w(std::move(f)); 
    } 
}; 

} // detail 

template<typename F, typename W> 
auto then(F f, W w) -> std::future<decltype(w(std::move(f)))> 
{ 
    return std::async(std::launch::async, 
     detail::helper<F, W, decltype(w(std::move(f)))>(std::move(f), 
                 std::move(w))); 
} 

int 
test() 
{ 
    return 1; 
} 

int 
main() 
{ 
    std::future<int> f = std::async(test); 
    auto f2 = then(std::move(f), [](std::future<int> f) 
    { 
     return f.get() * 2; 
    }); 
}