简体   繁体   English

g ++和clang ++不同的行为推导可变参数模板`auto`值

[英]g++ and clang++ different behaviour deducing variadic template `auto` values

Another "who's right between g++ and clang++?" 另一个“g ++和clang ++之间谁是正确的?”

This time I'm convinced it's a g++ bug, but I ask for a confirm from standard gurus. 这次我确信这是一个g ++错误,但我要求标准专家确认。

Given the following code 给出以下代码

template <template <auto...> class Cnt,
          typename ... Types,
          Types ... Vals>
void foo (Cnt<Vals...>)
 { }

template <auto ...>
struct bar
 { };

int main ()
 {
   foo(bar<0, 1>{});  // compile both
   foo(bar<0, 1L>{}); // only clang++ compile; error from g++
 }

Live demo 现场演示

clang++ (8.0.0, by example) compile and link without problem where g++ (9.2.0, by example) gives the following error compiling the second foo() (but not the first) call clang ++(8.0.0,通过示例)编译和链接没有问题g ++(9.2.0,通过示例)给出以下错误编译第二个foo() (但不是第一个)调用

prog.cc: In function 'int main()':
prog.cc:16:20: error: no matching function for call to 'foo(bar<0, 1>)'
   16 |    foo(bar<0, 1L>{}); // only clang++ compile; error from g++
      |                    ^
prog.cc:6:6: note: candidate: 'template<template<auto ...<anonymous> > class Cnt, class ... Types, Types ...Vals> void foo(Cnt<Vals ...>)'
    6 | void foo (Cnt<Vals...>)
      |      ^~~
prog.cc:6:6: note:   template argument deduction/substitution failed:
prog.cc:16:20: note:   mismatched types 'int' and 'long int'
   16 |    foo(bar<0, 1L>{}); // only clang++ compile; error from g++
      |                    ^
prog.cc:16:20: note:   'bar<0, 1>' is not derived from 'Cnt<Vals ...>'

If I understand correctly, g++ require that the Types... of the Vals... coincide where clang++ accept that the Vals... are of different Types... . 如果我理解正确的话,g ++要求Vals...Types...在clang ++接受Vals...属于不同Types...重合Types...

Who's right? 谁是对的?

-- EDIT -- - 编辑 -

As pointed by Marek R (thanks) also MSVC (v19.22) fail to compile. 正如Marek R所指出的那样(感谢),MSVC(v19.22)也无法编译。

But, if I understand correctly, fail also compiling the first foo() call with the following error 但是,如果我理解正确,那么也会因为以下错误而编译第一个foo()调用失败

<source>(13): error C2672: 'foo': no matching overloaded function found
<source>(13): error C2893: Failed to specialize function template 'void foo(Cnt<Vals...>)'
<source>(13): note: With the following template arguments:
<source>(13): note: 'Cnt=bar'
<source>(13): note: 'Types={}'
<source>(13): note: 'Vals={0, 1}'

-- EDIT 2 -- - 编辑2 -

camp0 observes (thanks) that g++ compile this code until version 7.4. camp0观察(谢谢)g ++编译此代码直到版本7.4。

A bug introduced from 8.1 or is bugged my code and g++ has corrected his code from 8.1? 从8.1引入的错误或者我的代码和g ++的错误已经从8.1修正了他的代码?

None of the three compilers is correct. 这三个编译器都不正确。

From [temp.param]/17 : 来自[temp.param] / 17

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. 如果template-parameter是一个类型参数 ,在其可选标识符之前带有省略号,或者是声明包([dcl.fct])的参数声明 ,则template-parameter是模板参数包。 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中声明的模板参数包 [ Example: [例如:

... ...

 template <class... T, T... Values> // error: Values expands template type parameter struct static_array; // pack T within the same template parameter list 

— end example ] - 结束例子]

So the code is ill-formed, even without the line foo(bar<0, 1L>{}); 所以即使没有行foo(bar<0, 1L>{});代码也是格式错误的foo(bar<0, 1L>{}); .

There is already a Clang bug report and a GCC bug report . 已经有一个Clang 错误报告和一个GCC 错误报告

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

相关问题 g ++和clang ++使用指向可变参数模板函数的指针的不同行为 - g++ and clang++ different behaviour with pointer to variadic template functions g ++和clang ++使用自动参数的模板特化的不同行为 - g++ and clang++ different behaviour with template specialization for auto argument g++ 和 clang++ 与可变参数容器的不同行为 - g++ and clang++ different behaviour with variadic container g ++和clang ++使用变量模板和SFINAE的不同行为 - g++ and clang++ different behaviour with variable template and SFINAE 带有集成模板参数的g ++和clang ++不同的行为 - g++ and clang++ different behaviour with integral template parameter 使用lambdas的变量模板:使用g ++但使用clang ++运行时出错 - Variadic template using lambdas : error with g++ but running with clang++ 可变参数模板元编程:clang ++或g ++中的错误? - Variadic template metaprogramming : a bug in clang++ or g++? 空包装的Functor可变参数模板包装扩展在clang ++和g ++中给出了不同的结果 - Functor variadic template pack expansion for empty pack gives different results in clang++ and g++ g ++和clang ++在模板类中定义的朋友模板函数的不同行为 - g++ and clang++ different behaviour with friend template function defined inside a template class g ++和clang ++不同的行为推断函数的模板返回类型 - g++ and clang++ different behaviour inferring the template return type of a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM