packaged_task で非同期を実装しようとしています。テンプレート化された関数 bsync を使用してこれを試みています。bsync は、関数 f とパラメータ パック args の 2 つの引数を取り、future を返します。future は f(args...) によって返される型です。つまり、リターンは未来です
私はほとんどそこにいると思いますが、型変換エラーが発生しています。どんな助けでも大歓迎です:
#include "stdafx.h"
#include <iostream>
#include <future>
#include <thread>
#include <functional>
#include <type_traits>
using namespace std;
//Implement bsync as a templated function
//Template parameters are (i) Fn (the function to run), (ii) Args, a parameter pack of arguments to feed the function
//The function returns a future<Ret>, where Ret is the return-type of Fn(Args)
template<class Fn,class...Args>
auto bsync(Fn f, Args&&...args)->future<result_of<decltype(f)&(Args&&...)>>{
//Determine return-type
typedef result_of<decltype(f)&(Args&&...)>::type A;
//Initialize a packaged_task
packaged_task <A(Args&&...)>tsk(f);
//Initialize a future
future<A> fut = tsk.get_future();
//Run the packaged task in a separate thread
thread th(move(tsk),(args)...);
//Join the thread
th.join();
return fut;
}
int plus_one(int x){
cout << "Adding 1 to " << x << endl;
return x++;
}
int main(){
auto x = bsync(plus_one, 1);
cout << "Press any key to continue:" << endl;
cin.ignore();
return 0;
}