Search code examples
c++gcccompiler-errorsclangvariadic-templates

Why must the template parameter pack be last?


I am facing the following inconsistency between gcc10.2 and clang11:

template<typename ... Args, typename T>
static constexpr int fuu = sizeof(T);

clang complains that a template pack must be the last thing in the template declaration:

error: template parameter pack must be the last template parameter
template<typename ... Args, typename T>  

but gcc is cool with it.
See https://godbolt.org/z/v9KeW6

Is this "last thing" actually a rule? In a function this works with both compilers.

template<typename... Args, typename T>
int foo(T) {
    return sizeof(T);
}

I don't see a reason why the first snippet shouldn't work, the template arguments are unambiguously deducible.


Solution

  • The variable template declaration is ill-formed, and it's a gcc bug for not diagnosing it.

    From temp.param#14:

    ... If a template-parameter of a primary class template, primary variable template, or alias template is a template parameter pack, it shall be the last template-parameter. ...

    The function template is fine, since the rules for function templates are different. If the template parameters after the parameter pack can be deduced by the arguments provided at the call site, then the template is ok.