C++ 将打包的任务移动到lambda中

C++ 将打包的任务移动到lambda中,c++,lambda,c++11,packaged-task,C++,Lambda,C++11,Packaged Task,我想移动并调用lambda中的boost::packaged_任务 然而,我想不出一个优雅的解决方案 e、 这不会编译 template<typename Func> auto begin_invoke(Func&& func) -> boost::unique_future<decltype(func())> // noexcept { typedef boost::pa

我想移动并调用lambda中的boost::packaged_任务

然而,我想不出一个优雅的解决方案

e、 这不会编译

        template<typename Func>
        auto begin_invoke(Func&& func) -> boost::unique_future<decltype(func())> // noexcept
        {   
            typedef boost::packaged_task<decltype(func())> task_type;

            auto task = task_type(std::forward<Func>(func));
            auto future = task.get_future();

            execution_queue_.try_push([=]
            {
                try{task();}
                catch(boost::task_already_started&){}
            });

            return std::move(future);       
        }

    int _tmain(int argc, _TCHAR* argv[])
    {
        executor ex;
        ex.begin_invoke([]{std::cout << "Hello world!";});
       //error C3848: expression having type 'const boost::packaged_task<R>' would lose some const-volatile qualifiers in order to call 'void boost::packaged_task<R>::operator ()(void)'
//          with
//          [
//              R=void
//          ]
        return 0;
    }

将打包任务移动到lambda中的“正确”方法是什么?

关于移动到lambda中,我提出了一个类似的问题。C++0x没有任何移动捕获语法。我能想出的唯一解决办法是某种代理函数对象

template<typename T, typename F> class move_capture_proxy {
    T t;
    F f;
public:
    move_capture_proxy(T&& a, F&& b) 
        : t(std::move(a)), f(std::move(b)) {}
    auto operator()() -> decltype(f(std::move(b)) {
        return f(std::move(b));
    }
};
template<typename T, typename F> move_capture_proxy<T, F> make_move_proxy(T&& t, F&& f) {
    return move_capture_proxy<T, F>(std::move(t), std::move(f));
}

execution_queue.try_push(make_move_proxy(std::move(task), [](decltype(task)&& ref) {
    auto task = std::move(ref);
    // use task
});
模板类移动\u捕获\u代理{
T;
F;
公众:
移动捕获代理(T&a、F&b)
:t(std::move(a)),f(std::move(b)){}
自动运算符()()->decltype(f(std::move(b)){
返回f(标准::移动(b));
}
};
模板移动\捕获\代理制作\移动\代理(T&&T、F&&F){
返回move_capture_proxy(std::move(t),std::move(f));
}
执行队列。尝试推送(使移动代理(std::move(task),[](decltype(task)&&ref){
自动任务=标准::移动(参考);
//使用任务
});

请注意,我实际上还没有尝试过这段代码,使用可变模板会更好,但MSVC10没有它们,所以我并不真正了解它们。

通过正确实现std::bind(或与启用移动的类型等效的东西),您应该能够将bind和C++0x lambda结合起来,如下所示:

task_type task (std::forward<Func>(func));
auto future = task.get_future();

execution_queue_.try_push(std::bind([](task_type const& task)
{
    try{task();}
    catch(boost::task_already_started&){}
},std::move(task)));

return future;
task_类型任务(std::forward(func));
自动未来=任务。获取未来();
执行队列尝试推送(std::bind([])(任务类型const&task)
{
试试{task();}
catch(boost::task_已启动&){
},std::move(task));
回归未来;

顺便说一句:你不需要std::move-around-future,因为future是一个本地对象。因此,它已经受到潜在的复制省略的影响,如果编译器不能做到这一省略,它就必须move从“future”构造返回值。在这种情况下显式使用std::move实际上可能会抑制复制/移动省略。

类似于I a实际上,在您的问题中添加了一个涉及std::bind的解决方案:另外,您似乎忘记了使完美转发工作正常的模板参数推导规则。几个std::move应该分别替换为std::forward和一个decay::type decay::type不会有任何影响。我希望您知道,t和F可能会被推导为to是左值引用。不幸的是,这在MSVC2010上似乎不起作用。std::bind似乎使用result_of而不是decltype。如果result_of使用decltype,则使用result_of也可以。;-)
task_type task (std::forward<Func>(func));
auto future = task.get_future();

execution_queue_.try_push(std::bind([](task_type const& task)
{
    try{task();}
    catch(boost::task_already_started&){}
},std::move(task)));

return future;