简体   繁体   English

GCC & Clang vs MSVC Bug,同时在函数模板的相同参数子句中扩展模板参数包

[英]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不能在自己的参数列表中展开。


Are the below given examples valid/invalid?以下给出的示例是否有效/无效?

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.

相关问题 MSVC 与 Clang/GCC 错误在函数模板的重载解决期间,其中一个包含参数包 - MSVC vs Clang/GCC bug during overload resolution of function templates one of which contains a parameter pack GCC vs Clang & MSVC 使用非类型模板参数时出现错误 - GCC vs Clang & MSVC Bug while using non type template parameter Consexpr function 对模板参数 object 的评估(MSVC 与 clang/gcc) - Constexpr function evaluation on a template parameter object (MSVC vs clang/gcc) gcc vs clang:将捕获的参数包扩展两次 - gcc vs clang: expanding a captured parameter pack twice 使用fold表达式将参数包扩展为lambda - gcc vs clang - Expanding parameter pack into lambda with fold expression - gcc vs clang Clang vs GCC - Variadic模板参数包后跟带默认值的参数适用于GCC 4.8但不适用于Clang 3.5 - Clang vs GCC - Variadic template parameter pack followed by parameter with default value works in GCC 4.8 but not Clang 3.5 模板参数包在Clang上失败但在VS 2015上失败 - Template Parameter Pack Fails on Clang but not VS 2015 MSVC vs GCC & Clang 使用 lambda 时的错误 - MSVC vs GCC & Clang Bug while using lambdas 使用 constexpr 函数的结果作为模板参数(clang vs gcc) - using result of constexpr function as a template parameter (clang vs gcc) 当模板模板参数的模板参数为包扩展时,gcc失败,clang成功 - when template parameter of a template template-parameter is pack expansion, gcc fails, clang succeeds
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM