1

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:

  • I dont completely understand why UpdateStuff<TT...>(); doesn't work, if TT is just double at compile-time why doesnt it use the first function?
  • Is it possible to do what I am trying to accomplish without function parameters?

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.

Bollafa
  • 23
  • 6
  • My guess is that `UpdateStuff();` would match both overloads of the `UpdateStuff` function as `TT` will be deduced there as an empty pack... – W.F. Jul 05 '16 at 13:50

1 Answers1

1

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...>();
}
max66
  • 65,235
  • 10
  • 71
  • 111