简体   繁体   English

为什么模板参数包必须放在最后?

[英]Why must the template parameter pack be last?

I am facing the following inconsistency between gcc10.2 and clang11 :我在gcc10.2clang11之间面临以下不一致:

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: clang抱怨模板包必须是模板声明中的最后一件事:

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

but gcc is cool with it.但是gcc很酷。
See https://godbolt.org/z/v9KeW6https://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.我看不出第一个片段不应该工作的原因,模板参数是明确可推论的。

The variable template declaration is ill-formed, and it's a gcc bug for not diagnosing it.变量模板声明格式错误,这是一个 gcc 错误,无法诊断它。

From temp.param#14 :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.如果参数包后面的模板参数可以通过调用点提供的参数推导出来,那么模板就ok了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM