Search code examples
concurrencystdthread

std::move of std::packaged_task<void()> does not compile (vs2013)


std::move doe's not compile when moving an std::packaged_task<void()> object.

the errors are:

error C2182: '_Get_value' : illegal use of type 'void'
error C2182: '_Val' : illegal use of type 'void'
error C2182: '_Val' : illegal use of type 'void' error C2665: 'std::forward': none of the 2 overloads could convert all the argument types
error C2512: 'std::_Promise': no appropriate default constructor available

The code is:

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);

This code is successfully compiled:

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


Solution

  • This is visual studio compiler bug, I found a nice solution by implementing packaged_task here