繁体   English   中英

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

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

另一个“g ++和clang ++之间谁是正确的?”

这次我确信这是一个g ++错误,但我要求标准专家确认。

给出以下代码

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++
 }

现场演示

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 ...>'

如果我理解正确的话,g ++要求Vals...Types...在clang ++接受Vals...属于不同Types...重合Types...

谁是对的?

- 编辑 -

正如Marek R所指出的那样(感谢),MSVC(v19.22)也无法编译。

但是,如果我理解正确,那么也会因为以下错误而编译第一个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}'

- 编辑2 -

camp0观察(谢谢)g ++编译此代码直到版本7.4。

从8.1引入的错误或者我的代码和g ++的错误已经从8.1修正了他的代码?

这三个编译器都不正确。

来自[temp.param] / 17

如果template-parameter是一个类型参数 ,在其可选标识符之前带有省略号,或者是声明包([dcl.fct])的参数声明 ,则template-parameter是模板参数包。 模板参数包是一个参数声明,其类型包含一个或多个未展开的包,是包扩展。 ... 作为包扩展的模板参数包不应扩展在同一template-parameter-list中声明的模板参数包 [例如:

...

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

- 结束例子]

所以即使没有行foo(bar<0, 1L>{});代码也是格式错误的foo(bar<0, 1L>{});

已经有一个Clang 错误报告和一个GCC 错误报告

暂无
暂无

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

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