std :: move of std :: packaged_task <void()>无法编译(vs2013)

时间:2015-08-27 12:42:07

标签: concurrency stdthread

移动std::move对象时,

std::packaged_task<void()> doe无法编译。

错误是:

错误C2182:'_ Get_value':非法使用'void'类型 错误C2182:'_ VAL':非法使用'void'类型 错误C2182:'_ VAL':非法使用'void'类型 错误C2665:'std :: forward':2个重载中没有一个可以转换所有参数类型
错误C2512:'std :: _ Promise':没有合适的默认构造函数

代码是:

struct CalcFib
{
    int m_num;
    int m_res;
    CalcFib(int number) :m_num(number)
    {

    }

    CalcFib() :m_num(0)
    {

    }

    void  operator()()
    {
        m_res = fib(m_num);
    }

    int fib(int num)
    {
        if (num < 2) return num;
        else return fib(num - 1) + fib(num - 2);
    }

};

std::packaged_task<void()> task(std::move(CalcFib(30)));

std::packaged_task<void()> task1 = std::move(task);

此代码已成功编译:

struct CalcFib
{
    int m_num;
    int m_res;
    CalcFib(int number) :m_num(number)
    {

    }

    CalcFib() :m_num(0)
    {

    }

    int  operator()()
    {
        m_res = fib(m_num);
        return m_res;
    }

    int fib(int num)
    {
        if (num < 2) return num;
        else return fib(num - 1) + fib(num - 2);
    }

};

std::packaged_task<int()> task(std::move(CalcFib(30)));

std::packaged_task<int()> task1 = std::move(task);

similar issue was asked here but has not answer

1 个答案:

答案 0 :(得分:0)

这是visual studio编译器错误,我通过实现packaged_task here找到了一个很好的解决方案