templatesc++11metaprogrammingvariadic-functionsvariable-templates

Giving Variadic Template Pack as function specification


I'm trying to create a function that has a template pack specialization (it has no parameters) and it prints a message, until the last one where function specialization makes it print something else, and stop. As I'm really bad explaining , here I post the code of what I'm trying to do:

template <typename T>
constexpr void UpdateStuff()
{
    std::cerr << "I am the last one :D" << std::endl;

}
template< typename T ,typename... TT>
constexpr void UpdateStuff()
{



    std::cerr << "I am NOT the last one :D";

    UpdateStuff<TT...>();

}
int main()
{


    UpdateStuff<int,double>(); // Should only print text twice
    std::cin.get();

    return 1;
}

As a first note , I know this does not work, UpdateStuff<TT...>(); it yields an ambigous call to overloaded function , I've managed to get it working by giving the function UpdateStuff() parameters like UpdateStuff(T first, TT... second) and the specialization only one UpdateStuff(T first) but I want to know if this is possible without function parameters , I'll give a summary of my questions:

Thanks in advance , and if you don't understand something related to my question I'd be grateful to explain it better,and sorry for my poor english.


Solution

  • Yes, it's possible.

    But take in count that typename ... TT is "zero or more typenames", so calling UpdateStaff<double>() both version of UpdateStaff() are ok.

    You can, by example, impose a second type in the not final version; something like this

    template <typename T1, typename T2, typename... TT>
    constexpr void UpdateStuff()
    {
        std::cerr << "I am NOT the last one :D";
    
        UpdateStuff<T2, TT...>();
    }