Stefan
Stefan

Reputation: 3859

Why do I get an "variable 'std::packaged_task<int> task' has initializer but incomplete type" error

I have the below code which gives the error mentioned in the title. It is a cut down version of the sample available here:

cppreference

#include <thread>
#include <future>

int main()
{
    std::packaged_task<int()> task([] {return 1;});
    return 0;
}

However, thought that being as I was specifying the type of the package task as int() as specified in another answer here then it would be correctly resolved but it is not.

Can anyone spot what I have done wrong?

Upvotes: 3

Views: 687

Answers (1)

green lantern
green lantern

Reputation: 1095

try:

g++ -E x.cpp > output.txt

It will run the preprocessor so you can see what the compiler is getting.

If I run it from cygwin, only the predeclaration of std::packaged_task is present in the output, but not the definition. The future file contains the following preprocessor condition:

#if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) \
  && defined(_GLIBCXX_ATOMIC_BUILTINS_4)

and in my cygwin installation, the macro _GLIBCXX_HAS_GTHREADS is not defined, so everything inside the #if is removed. Perhaps the same happens to you.

Upvotes: 1

Related Questions