0

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

Community
  • 1
  • 1
aviyaChe
  • 145
  • 1
  • 9
  • Can you add compiler version and system? This compiles fine with clang 3.4.1 (`-std=c++11`) and gcc 4.8.5. – dhke Aug 27 '15 at 12:46
  • Microsoft (R) C/C++ Optimizing Compiler Version 18.00.40629 for x86 – aviyaChe Aug 27 '15 at 12:56
  • This is a bug of visual studio compiler, fount a nice solution [here][1] [1]: http://stackoverflow.com/questions/14744588/why-is-stdpackaged-taskvoid-not-valid – aviyaChe Aug 30 '15 at 07:23

1 Answers1

0

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

Community
  • 1
  • 1
aviyaChe
  • 145
  • 1
  • 9