1

私はこれをコンパイルしようとしました (いくつかの小さな明らかな修正を加えて) c++11 非同期継続または libc++ で clang (最新バージョン) を使用して .then() セマンティクスを試みまし たが、コンパイルされません: No matching function for call to 'それから'。

理由がわかりませんでした... これで私を助けてくれますか?

4

1 に答える 1

2

いくつかの場所で答えが欠け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; 
    });
}
于 2013-04-25T15:04:00.273 に答える