[英]GCC & Clang vs MSVC Bug while expanding template parameter pack in the same parameter clause for function templates
I came across the following statement in the standard :我在标准中遇到了以下声明:
If a template-parameter is a type-parameter with an ellipsis prior to its optional identifier or is a parameter-declaration that declares a pack ([dcl.fct]), then the template-parameter is a template parameter pack.
如果模板参数是在其可选标识符之前带有省略号的类型参数,或者是声明包([dcl.fct])的参数声明,则模板参数是模板参数包。 A template parameter pack that is a parameter-declaration whose type contains one or more unexpanded packs is a pack expansion.
作为参数声明的模板参数包,其类型包含一个或多个未扩展包,是包扩展。 ... A template parameter pack that is a pack expansion shall not expand a template parameter pack declared in the same template-parameter-list .
...作为包扩展的模板参数包不应扩展在同一template-parameter-list 中声明的模板参数包。
(end quote) (结束报价)
So consider the following invalid example:因此,请考虑以下无效示例:
template<typename... Ts, Ts... vals> struct mytuple {}; //invalid
The above example is invalid because the template type parameter pack Ts
cannot be expanded in its own parameter list.上面的例子是无效的,因为模板类型参数包
Ts
不能在自己的参数列表中展开。
Then i tried the same with function templates and expected the same result but to my surprise it compiles fine in gcc & clang but not in msvc.然后我对函数模板进行了同样的尝试,并期望得到相同的结果,但令我惊讶的是,它在 gcc 和 clang 中编译得很好,但在 msvc 中却没有。 The example is as follows:
示例如下:
//is this valid?
template<typename... T, T... ar>
void func(){}
int main()
{
}
Similarly, the below given example compiles in gcc and clang but not in msvc:同样,下面给出的示例在 gcc 和 clang 中编译,但不在 msvc 中:
//is this valid?
template<typename...T, int (*FUNC)(T...)>
int wrapper(T... args) { return (*FUNC)(args...) * 10; }
int main()
{
}
Which compiler is right here?哪个编译器在这里? That is, does the quoted statement temp.param#17 applies to the given two examples and so they are invalidated or is the quote not applicable to the given two examples.
也就是说,引用的语句 temp.param#17 是否适用于给定的两个示例,因此它们无效,或者引用不适用于给定的两个示例。
It's a bug in GCC and Clang.这是 GCC 和 Clang 中的一个错误。 You wrote clearly-defined-as-invalid code, MSVC correctly throws an error while gcc and clang do not.
您编写了明确定义为无效的代码,MSVC 正确地引发了错误,而 gcc 和 clang 则没有。 Therefore MSVC is correct.
因此 MSVC 是正确的。 GCC and Clang are not.
GCC 和 Clang 不是。
As an aside, I ran the code snippets in MSVC C++20 mode and the same error was still thrown.顺便说一句,我在 MSVC C++20 模式下运行了代码片段,但仍然抛出了同样的错误。 It's been correctly handled since C++14 in MSVC and a bug in GCC and Clang for just as long.
自 MSVC 中的 C++14 和 GCC 和 Clang 中的错误以来,它一直得到正确处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.