I want to be able to call a function with a template parameter that is of function type (including parameter and return types), i.e. double(int, long)
, and in the function separate the types and use them individually.
For example I want to be able to call a function
printRes<double(int, long)>();
This function above should parse the template argument and extract the return type double
and output it.
I know how to do this using a class and variadic templates:
#include <iostream>
#include <typeinfo>
template <typename T>
class A {};
template <typename Res, typename... Args>
class A<Res (Args...)> { // Parse template argument
public:
void printRes() {
std::cout << typeid(Res).name() << std::endl;
}
};
Then I can use it like this:
int main() {
A<double(int, long)> a;
a.printRes();
}
Which outputs:
d
I want to do this using a simple function instead. This is what I came up with:
template <typename Res, typename... Args>
void printRes() {
std::cout << typeid(Res).name() << std::endl;
}
However, now I must specify the template parameters like this instead:
int main() {
printRes<double, int, long>();
}
Is there any way to implement the function so it can be called using the same template parameter as the class-version (i.e. double(int, long)
)?