If you really want to use the template to accept any signature of any type, then the implementation should be something like this:
class A { public: template<typename F, typename... Args> auto f(F&& funct, Args&&... args) { return funct(std::forward<Args...>(args)...); } };
This is because you said in the comment:
Q: Is type F in the class required only for method F ?
A: method only.
Because of this, it is useless to have a template class when you can just have a template method.
Here is an example of how to call a method that simply calls the "called object with its parameters", in this case a lambda function:
int main(int argc, char* argv[]) { A a; af([](int i) -> int { return i + 5; }, 12); // |------callable object-----------| |argument of function| return 0; }
In practice, the F method accepts the βcalled objectβ as the first argument and asks for any parameters to call the first argument as additional arguments.
Additional notes:
If you want to pass a certain type of function signature to the F method, for example: int (*)(int) , then you can avoid using a template and pass an object of type std::function .
This is just an example:
#include <functional> class A { public: // method g accept a function which get a integer and return an integer as well. int g(std::function<int(int)> funct, int arg) { return funct(arg); } };
Biagio festa
source share