mardi 5 juillet 2016

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:

  • 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.

Aucun commentaire:

Enregistrer un commentaire