recursive variadic template to print out the contents of a parameter pack

How is it possible to create a recursive variadic template to print out the contents of a paramater pack? I am trying with this, but it fails to compile:

template <typename First, typename ...Args>
std::string type_name () {
    return std::string(typeid(First).name()) + " " + type_name<Args...>();
}
std::string type_name () {
    return "";
}

How shall I end the recursion?


There's actually a very elegant way to end the recursion:

template <typename Last>
std::string type_name () {
    return std::string(typeid(Last).name());
}

template <typename First, typename Second, typename ...Rest>
std::string type_name () {
    return std::string(typeid(First).name()) + " " + type_name<Second, Rest...>();
}

I initially tried template <typename Last> and template <typename First, typename ...Rest> but that was considered ambiguous (Rest can be zero elements). This question then showed me the definitive solution: Compilation Error on Recursive Variadic Template Function


Note, to avoid a bit of code duplication, you could also do:

template <typename Last>
std::string type_name () {
    return std::string(typeid(Last).name());
}

template <typename First, typename Second, typename ...Rest>
std::string type_name () {
    return type_name<First>() + " " + type_name<Second, Rest...>();
}

You need to use partial specialisation to end the recursion, but since you can't partially specialise free functions in C++, you need to create an implementation class with a static member function.

template <typename... Args>
struct Impl;

template <typename First, typename... Args>
struct Impl<First, Args...>
{
  static std::string name()
  {
    return std::string(typeid(First).name()) + " " + Impl<Args...>::name();
  }
};

template <>
struct Impl<>
{
  static std::string name()
  {
    return "";
  }
};

template <typename... Args>
std::string type_name()
{
    return Impl<Args...>::name();
}

int main()
{
  std::cout << type_name<int, bool, char, double>() << std::endl; // "i b c d"
  return 0;
}

That first declaration of Impl is just a workaround for a shortcoming in g++ 4.6 (and below). It won't be necessary once it implements variadic templates correctly.

Check it out in action at ideone.com


C++17's if constexpr allows you to do this in one template declaration which is, unlike a lot of the older solutions, pretty easy to understand:

template <typename T, typename ...Args>
std::string type_name() {
  if constexpr (!sizeof...(Args)) {
    return std::string(typeid(T).name());
  } else {
    return std::string(typeid(T).name()) + " " + type_name<Args...>();
  }
}

As an alternative to non-existing partial specialization for functions, you can use overloading on a typifier class:

#include <string>
#include <iostream>
#include <typeinfo>

template <unsigned int N> struct NumberToType { };

template <typename T>
std::string my_type_name(NumberToType<0> = NumberToType<0>())
{
  return std::string(typeid(T).name());
}

template <typename T, typename ...Args>
std::string my_type_name(NumberToType<sizeof...(Args)> = NumberToType<sizeof...(Args)>())
{
  return std::string(typeid(T).name()) + " " + my_type_name<Args...>(NumberToType<sizeof...(Args)-1>());
}

int main()
{
  std::cout << my_type_name<int, double, char>() << std::endl;
}