Reputation: 4251
I'm trying to find a way to extract the type and number of arguments and return type function pointer so I can write something like this:
int foo(int x, int y)
{
return x+y;
}
template<class T, class U>
U callfunc(T, ...);
int main()
{
int k = callfunc(&foo, 3, 5);
}
callfunc
will check if the number of arguments and the types are correct and return the result. But how should it be implemented?
I've googled a bit and found some articles covering similar topics using macros and templates but still can't do it.
Can someone provide some pointers?
Thanks!
P.S: I can't use variadic templates because VS2012 doesn't support it.
Upvotes: 2
Views: 744
Reputation: 283684
You'll want some variadic templates, something like:
template<typename TResult, typename ...TArgs>
TResult callfunc(TResult (*fn)(TArgs...), TArgs... args)
{
/* do something with the args, if you wanted */
TResult result = fn(std::forward(args)...);
/* do something to the return value, if you wanted */
return std::forward(result);
}
Upvotes: 2