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

c++ variadic templates unpack
variadic template constructor
parameter packs not expanded with
c iterate over parameter pack
variadic template template parameter
expression contains unexpanded parameter pack
c++ template recursion
c++ variadic template printf

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?


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++ variadic template recursive example, Table of Contents Variadic templates allow you to have a template with a variable It took me a while to find out how to use the argument pack. Newline printing is omited by appending the comma ( , ) to the print()  Variadic templates allow you to have a template with a variable number of arguments, also called a parameter pack. Unpacking that pack is more difficult than it should be, so we use a recursive template to iterate over all the parameters one by one.


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...>();
}

Parameter pack(since C++11) - cppreference.com, This is a friendly introduction to variadic templates (and thereby, variadic What are the different syntactic elements, and what are they called? An important side note: it's possible for a template parameter pack to contain 0 types. You need to write such functions recursively – with a 'base' case, and a  Variadic Template vs Fold Expression. There is two way to process C++ parameter pack i.e. 1. Recursion 2. Fold Expression(From C++17) At whatever point conceivable, we should process a parameter pack with fold expression instead of using recursion. Because it has some benefits as: 1. Less code to write 2.


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;
}

Introduction to C++ Variadic Templates · R and C++, This function really does nothing but accept any argument we might pass to it and The traditionall approach to this would be to take out recursion and throw that at fact that we may apply functions to each of the elements in the tail (like fun(t). Back to the initial problem: We wanted to print a variadic number of things to a​  Pretty-print C++ STL containers vous pouvez décompresser le paramètre pack en place, comme dans l'exemple suivant: c++11 variadic-templates recursion typeid.


As an alternative, you can unpack the parameter pack in-place as in the following example:

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

template <typename T, typename ...Args>
std::string type_name () {
    std::string str = typeid(T).name();
    int arr[] = { 0, (str += std::string{" "} + typeid(Args).name(), 0)... };
    (void)arr;
    return str;
}

int main() {
    auto str = type_name<int, double, char>();
    std::cout << str << std::endl;
}

Recursion is not required actually to do that.

Ellipsis and Variadic Templates, In computer programming, variadic templates are templates that take a variable number of C++11 allows template definitions to take an arbitrary number of arguments of any type. Using the parameter pack, the user can bind zero or more arguments to the The use of variadic templates is often recursive. Print/​export. A template parameter pack is a template parameter that accepts zero or more template arguments (non-types, types, or templates). A function parameter pack is a function parameter that accepts zero or more function arguments. A template with at least one parameter pack is called a variadic template.


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...>();
  }
}

Using Variadic Templates cleanly, Args is called a template parameter pack, and Args args is called a Variadic templates are written just the way you'd write recursive code - you Here's a somewhat more interesting display of pattern matching: Since it returns a reference, we can use get to both read tuple elements and write to them:. Variadic templates. One of the new features of C++11 is variadic templates.Finally, there's a way to write functions that take an arbitrary number of arguments in a type-safe way and have all the argument handling logic resolved at compile-time, rather than run-time.


Variadic template, Today I want to show you a few techniques of variadic templates, that can be showRecursive is a function that prints its arguments, each in a separate line. Since we can't create an initializer list out of the parameter pack on the parameter pack will tell you the number of its elements at compile time. By using them Args, or args becomes a parameter pack. To be more precise, Args i s a template parameter pack and args is a function parameter pack. You can only apply two operations to a parameter pack: you can pack or unpack it. If the ellipse is left of Args the parameter pack is packed; if the ellipse is right of Args the


Variadic templates in C++, I gave an introduction to variadic templates last week. of pack elements and works on both template parameter packs and function parameter  To unpack a parameter pack, use a templated function taking one (or more) parameters explicitly, and the ‘rest’ of the parameters as a template parameter pack. Recurse towards a base case: call your recursive function with the ‘rest…’ arguments, and let the compiler unpack your parameters in subsequent calls to your recursive function.


Variadic functions, Variadic recursion involves calling a variadic function template by using differing (almost always decreasing) numbers of arguments, and thereby stamping out a different signature every time. A "base case" is still required, but the nature of the recursion is different.