Search code examples
c++c++11templatesvariadic-templatestemplate-argument-deduction

Mixing variadic template values and variadic deduced types


Is the following perfectly defined by the standard ?

#include <iostream>

template <unsigned int... Values, class... Types>
void f(Types&&... values)
{
    std::cout<<sizeof...(Values)<<" "<<sizeof...(Types)<<std::endl;
}

int main()
{
    f<7, 5>(3);
    return 0;
}

It compiles well under g++ 4.8 but I wonder if it is normal.


Solution

  • From ISO C++ standard's current working draft 14.1 (11):

    A template parameter pack of a function template shall not be followed by another template >parameter unless that template parameter can be deduced from the parameter-type-list of >the function template or has a default argument

    In your case 'Types' is a function parameter pack and 'Values', that is a template parameter pack, can be always followed by a function parameter pack. Also this code works for the same reason:

    #include <iostream>
    
    template <class... Values, class... Types>
    void f(Types&&... values)
    {
        std::cout<<sizeof...(Values)<<" "<<sizeof...(Types)<<std::endl;
    }
    
    int main()
    {
        f<int, float>(-3, 5);
        return 0;
    }