又是一次关于"g++和clang++谁更正确"的讨论。
这一次我相信是g++的一个bug,但我想从标准大师那里确认一下。
下面是给定的代码:
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...
。
谁是对的?
--编辑--
如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纠正了他的代码?
msvc
报告了类似的问题(我提供了 godbolt 演示)。你需要寻求法律意见。 - Marek Rmsvc
在第一次调用时也失败了。 - max66