Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple pack expansions inside class with fixed number of template arguments

Is this code well-formed? The declaration of the function template itself gives an error in both clang and gcc even though Ts could well be empty.

// error: too many template arguments for class template 'pair'
template<class I, class U, class... Ts>
void f(std::pair<I,U,Ts...>);

int main()
{
    f(std::pair<int,int>());
}

The function call gives this error in gcc which doesn't make sense. There is no conversion to int:

note: cannot convert 'std::pair<int, int>()' (type 'std::pair<int, int>') to type 'int'
like image 914
template boy Avatar asked Sep 26 '22 14:09

template boy


1 Answers

[temp.res]/8:

If every valid specialization of a variadic template requires an empty template parameter pack, the template is ill-formed, no diagnostic required.

Every valid specialization of f would require Ts to be an empty pack. Therefore the program is ill-formed NDR. Both compilers are correct.

As to GCC's diagnostic, that seems to be due to its habit of using int as a placeholder for "something that looks like a type but doesn't make sense" for error recovery purposes.

like image 116
T.C. Avatar answered Nov 03 '22 00:11

T.C.