Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variadic templates, parameter pack and its discussed ambiguity in a parameter list

In this question, I'll refer to my previous question.

In that question, I found that the following is not valid:

template<typename T, typename... A, typename S>
class C { };

This is because:

[It is not valid code] for class templates because their arguments must always be specified, which will always result in an ambiguity unless the parameter pack is at the end and slurps up any remaining template parameters.

That makes sense, of course and I got it.

Then, as an alternative approach, the following that involves a specialization has been proposed:

template<typename F, typename S>
class C;

template<typename T, typename... A, typename S>
class C<T(A...), S> { };

Actually, it seems to work, so thanks to the one that proposed it.

Anyway, what I don't understand is why this is valid code while the previous one was not.
Should it suffer from the same ambiguity of the previous solution? Why and how does the compiler solve that ambiguity in that case?
According with the previous question (see the link at the start of this question), it seems to me that still the variadic part should slurp up any parameters to the end, thus this code should not be valid as well.
I'm wrong, of course, but what's wrong exactly in my reasoning?

like image 354
skypjack Avatar asked Jan 22 '16 23:01

skypjack


1 Answers

In the class template, a prospective template argument list C<a,b,c,d,e,f> needs to match

template<typename T, typename... A, typename S>

in which ...A is just floating in a parameter list.

But in the specialization, what needs to be matched is not the list of template variabled but rather the pattern:

C<T(A...), S>

which is easy because the A... is delimited.

So in the template specialization, the list of parameters is just an inventory of symbols, some scalar and some parameter packs, which will appear in a pattern.

like image 185
rici Avatar answered Sep 30 '22 18:09

rici