I'm trying to make a class that consume functions by istantiating asyncs. Thats my code:
main.cpp
MPIAsyncPool pool;
MPIFuture val = pool.addTask(launch::async, [](int a) -> int {
return a;
}, 7);
cout << "Value: " << val.get() << endl;
MPIFuture v = pool.addTask(std::launch::async, doThings, 46);
cout << "Value: " << v.get() << endl;
MPIAsyncPool(.h&.cpp)
class MPIAsyncPool {
public:
MPIAsyncPool();
template<typename T, typename U>
MPIFuture addTask(std::launch, std::function<T(U...)> f, U...);
};
template<typename ReturnType, typename ArgsTypes>
MPIFuture MPIAsyncPool::addTask(std::launch launch, std::function<ReturnType(ArgsTypes...)> f, ArgsTypes... args) {
std::future<int> fut = std::async(launch, f, args);
return MPIFuture(fut.get());
}
It seems that the declaration is ok, but I've some trouble with the definition signature. Am I wrong?
Those are the errors that I get:
/home/quero/ClionProjects/MPIAsyncPool/main.cpp: In function ‘int main(int, char**)’:
/home/quero/ClionProjects/MPIAsyncPool/main.cpp:17:15: error: no matching function for call to ‘MPIFuture::MPIFuture()’
MPIFuture val;
^
In file included from /home/quero/ClionProjects/MPIAsyncPool/MPIAsyncPool.h:11:0,
from /home/quero/ClionProjects/MPIAsyncPool/main.cpp:3:
/home/quero/ClionProjects/MPIAsyncPool/MPIfuture.h:12:5: note: candidate: MPIFuture::MPIFuture(int)
MPIFuture(int);
^
/home/quero/ClionProjects/MPIAsyncPool/MPIfuture.h:12:5: note: candidate expects 1 argument, 0 provided
/home/quero/ClionProjects/MPIAsyncPool/MPIfuture.h:10:7: note: candidate: constexpr MPIFuture::MPIFuture(const MPIFuture&)
class MPIFuture {
^
/home/quero/ClionProjects/MPIAsyncPool/MPIfuture.h:10:7: note: candidate expects 1 argument, 0 provided
/home/quero/ClionProjects/MPIAsyncPool/MPIfuture.h:10:7: note: candidate: constexpr MPIFuture::MPIFuture(MPIFuture&&)
/home/quero/ClionProjects/MPIAsyncPool/MPIfuture.h:10:7: note: candidate expects 1 argument, 0 provided
/home/quero/ClionProjects/MPIAsyncPool/main.cpp:20:13: error: no matching function for call to ‘MPIAsyncPool::addTask(std::launch, main(int, char**)::, int)’
}, 7);
^
In file included from /home/quero/ClionProjects/MPIAsyncPool/main.cpp:3:0:
/home/quero/ClionProjects/MPIAsyncPool/MPIAsyncPool.h:19:15: note: candidate: template MPIFuture MPIAsyncPool::addTask(std::launch, std::function, U, ...)
MPIFuture addTask(std::launch, std::function f, U...);
^
/home/quero/ClionProjects/MPIAsyncPool/MPIAsyncPool.h:19:15: note: template argument deduction/substitution failed:
/home/quero/ClionProjects/MPIAsyncPool/main.cpp:20:13: note: ‘main(int, char**)::’ is not derived from ‘std::function’
}, 7);
^
/home/quero/ClionProjects/MPIAsyncPool/main.cpp:22:64: error: no matching function for call to ‘MPIAsyncPool::addTask(std::launch, int (&)(int), int)’
MPIFuture v = pool.addTask(std::launch::async, doThings, 46);
^
In file included from /home/quero/ClionProjects/MPIAsyncPool/main.cpp:3:0:
/home/quero/ClionProjects/MPIAsyncPool/MPIAsyncPool.h:19:15: note: candidate: template MPIFuture MPIAsyncPool::addTask(std::launch, std::function, U, ...)
MPIFuture addTask(std::launch, std::function f, U...);
^
/home/quero/ClionProjects/MPIAsyncPool/MPIAsyncPool.h:19:15: note: template argument deduction/substitution failed:
/home/quero/ClionProjects/MPIAsyncPool/main.cpp:22:64: note: mismatched types ‘std::function’ and ‘int (*)(int)’
MPIFuture v = pool.addTask(std::launch::async, doThings, 46);
^
/home/quero/ClionProjects/MPIAsyncPool/MPIAsyncPool.cpp:14:109: error: expansion pattern ‘ArgsTypes’ contains no argument packs
MPIFuture MPIAsyncPool::addTask(std::launch launch, std::function f, ArgsTypes... args) {
^
/home/quero/ClionProjects/MPIAsyncPool/MPIAsyncPool.cpp:14:11: error: prototype for ‘MPIFuture MPIAsyncPool::addTask(std::launch, std::function)’ does not match any in class ‘MPIAsyncPool’
MPIFuture MPIAsyncPool::addTask(std::launch launch, std::function f, ArgsTypes... args) {
^
In file included from /home/quero/ClionProjects/MPIAsyncPool/MPIAsyncPool.cpp:5:0:
/home/quero/ClionProjects/MPIAsyncPool/MPIAsyncPool.h:19:15: error: candidate is: template MPIFuture MPIAsyncPool::addTask(std::launch, std::function, U, ...)
MPIFuture addTask(std::launch, std::function f, U...);
std::function, at best it can decay into a function pointer. You might be able to wrap the lambda in a{}to try to construct astd::functionin place. - Cory Kramer