C++0x 错误 : variable 'std::packaged_task<int> pt1' has initializer but incomplete type

标签 c++ multithreading c++11

下面是一个使用 packaged_task 和 futures 的 c++0x 简单程序。编译程序时出现错误:变量“std::packaged_task pt1”具有初始化程序但类型不完整

程序如下

#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;
}

最佳答案

类模板packaged_task对于一般情况是未定义的。仅定义了函数类型参数的部分特化。查看当前草稿:

template<class> class packaged_task; // undefined

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

由于您的 printFn函数不接受任何参数并返回 int你需要使用 packaged_task<int()>类型。

关于C++0x 错误 : variable 'std::packaged_task<int> pt1' has initializer but incomplete type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2953457/

相关文章:

c++ - clang:警告:没有这样的 sysroot 目录:编译 LLVM (3.9.0) 时为 '-mmacosx-version-min=10.5'

c++ - 在前往 C++ 之前,我应该首先学习什么?

c++ - Windows7内存管理——如何防止并发线程阻塞

python - 在 Python 中同时在控制台中打印 2 行

c++ - 如何释放线程本地存储的堆内存

特定类型的 C++ 迭代器

c++ - 按值类成员捕获

c++ - `std::map::at()`不支持std::string_view吗?

c# - 在访问变量之前检查锁定的静态分析工具

c++ - decltype 返回的模板推导/替换错误