代码如下所示,使用async生产一个future,定义了3个模板函数,func0接收一个future对象生产一个仿函数并执行,func1接收一个future直接调用函数执行,func2和func0处理方式一致,但是接收一个string对象,其中func1,func2可正常编译并执行,func3编译模板报错,报错信息如下图,请各位大大们能够解答一二。

#include <future>
#include <thread>
#include <unistd.h>
#include <iostream>
#include <functional>
using namespace std;

template<typename T>
void func0(T &&t)
{
        auto task = bind([](future<int> ft) -> int {
                ft.wait();
                cout << ft.get() << endl;
                return 0;
        }, forward<T>(t));
        task();
}

template<typename T>
void func1(T &&t)
{
        [](future<int> ft) -> int {
                ft.wait();
                cout << ft.get() << endl;
                return 0;
        } (forward<T>(t));
}

template<typename T>
void func2(T &&t)
{
        auto task = bind([](string s) -> int {
                cout << s << endl;
                return 0;
        }, forward<T>(t));
        task();
}
int main()
{
        auto ft = async([] () -> int {
                sleep(1);
                return 456;
        });
        string s = "123";
        func2(move(s));
        func1(move(ft));
        //func0(move(ft));   //编译报错
        cout << "wait" << endl;
        sleep(1);
}

 使用std::bind生成以std::future为参数的仿函数时编译报错

相关文章:

  • 2022-12-23
  • 2022-01-21
  • 2022-12-23
  • 2021-06-13
  • 2021-12-23
  • 2021-11-10
  • 2022-12-23
猜你喜欢
  • 2022-01-29
  • 2022-12-23
  • 2021-07-13
  • 2021-05-21
  • 2021-12-29
  • 2021-05-27
  • 2021-10-04
相关资源
相似解决方案