c++ - variadic template to call a function -
i trying write teamplate function looks this:
template<t funcptr, params...> void callfunction(params...) { funcptr(params...); } example usage:
typedef void (__stdcall* test_t)(int a1, bool a2, char* a3); test_t fn = ....; //pointer obtained somehow callfunction<fn>(10, true, "hello"); is possible? dont know how work parameter pack have unpacked each member of pack servers parameter.
i suggest minor rewrite this:
#include <iostream> template<class fun, class... args> void callfunction(fun fun, args&&... args) { fun(std::forward<args>(args)...); } void fn(int a1, bool a2, char const* a3) { std::cout << a1 << a2 << a3; } int main() { callfunction(fn, 10, true, "hello"); } live example. think can deduce proper ... syntax (after class... in paramter list, after arg... unpacking arguments @ call site.
the std::forward distinguish between lvalues , rvalues. it's better in general use argument deduction passing function pointer (or callable object in general) regular function argument, rather explicit template argument.
Comments
Post a Comment