views:

66

answers:

1

Hi All,

Below is a simple program in c++0x that makes use of packaged_task and futures. while compiling the program i get error : variable 'std::packaged_task pt1' has initializer but incomplete type

the program is below

#include <future>
#include <iostream>

using namespace std;


int printFn()
{

    for(int i = 0; i < 100; i++) 
    {

        cout << "thread " <<  i << endl;
    }
return 1;
}



int main()
{

   packaged_task<int> pt1(&printFn);

   future<int> fut =  pt1.get_future();

   thread t(move(pt1));

   t.detach();

   int value  = fut.get();

   return 0;
}
+1  A: 

The class template packaged_task is undefined for the general case. Only a partial specialization for function type parameters is defined. See the current draft:

template<class> class packaged_task; // undefined

template<class R, class... ArgsTypes>
class packaged_task<R(ArgTypes...)> {
   ...
   void operator()(ArgTypes...);
   ...
};

Since your printFn function doesn't take any parameters and returns an int you need to use the packaged_task<int()> type.

sellibitze