Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 642
Vincent Avatar asked Jan 13 '14 21:01

Vincent


1 Answers

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;
}
like image 57
Salvatore Avanzo Avatar answered Nov 15 '22 21:11

Salvatore Avanzo