My goal is to write a simple generic function for registering converters for arbitrary C++ types. For simplicity I'll just print C++ type names. I'd like to be able to call my generic print_type_name
function for any types, including multiple types at once (variadic):
template <typename T>
void print_type_name(void)
{
std::cout << typeid(T).name() << std::endl;
}
This works fine for things like this:
print_type_name<int>();
print_type_name<std::string>();
print_type_name<std::vector<std::complex<float> > >();
However, I need to be able to call this function for each type in a variadic template, e.g. (when expanded):
print_type_name<int, std::string, std::vector<std::complex<float> > >();
Here's what I've come up with, but it's rather clunky:
template <typename ...TS>
void noop(TS... ts) { }
template <typename T>
int real_print_type_name(void) {
std::cout << typeid(T).name() << std::endl;
return 0;
}
template <typename ...TS>
void print_type_name(void) {
noop(real_print_type_name<TS>()...);
}
Which allows for the following:
template <typename ...TS>
void other_function(void) {
print_type_name<TS...>();
}
Notice the useless noop
function and the int
return type of
real_print_type_name
, both of which I had to add in order to expand
the parameter pack. Is there a cleaner way of doing this?