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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With