Basically I just want to wrap any callable object and their arguments in a Task obj that can be called later. Here are the code that come to mind:
Lets say all those callable type have a member type can be seemed as if they are defined like this:
template<typename TReturn, typename...TArgs>
struct SomeFunction{
using ArgTypes = TArgs; // won't compile of course
}
And the Task Template can be defined like this:
template<typename TFunction>
class Task {
public:
Task(TFunction fun, typename TFunction::ArgTypes...args) // won't compile
: fun_(fun), args_(args){}
void operator()()
{
fun_(args_...); // won't compile: workaround 1
}
private:
typename TFunction::ArgTypes... args_; // won't compile: workaround 2
TFunction fun_;
};
The problem lies in the definition of Task's constructor. Is there any way to achieve it? Of course I can define it as a template constructor:
template<typename...TArgs>
Task(TFunction fun, TArgs...args)
But in this way the compiler won't know that the TArgs are the same as TFunction::ArgTypes. Consequently, the error messages are ridiculous when wrong arguments passed to it.
workaround 1 : C++ How to store a parameter pack as a variable
workaround 2 : Is it possible to "store" a template parameter pack without expanding it?