【发布时间】:2021-09-15 06:32:41
【问题描述】:
考虑以下程序:
template<typename T>
struct A { T t[2]; };
int main()
{
A a{1}; // only one value provided for 2-element array
return a.t[0];
}
在 C++20 模式下,它在 gcc 中编译良好,但在 Visual Studio 中失败并出现错误:
<source>(6): error C2641: cannot deduce template arguments for 'A'
<source>(6): error C2780: 'A<T> A(void)': expects 0 arguments - 1 provided
<source>(2): note: see declaration of 'A'
<source>(6): error C2784: 'A<T> A(A<T>)': could not deduce template argument for 'A<T>' from 'int'
<source>(2): note: see declaration of 'A'
<source>(6): error C2784: 'A<T> A(A<T>)': could not deduce template argument for 'A<T>' from 'int'
<source>(2): note: see declaration of 'A'
<source>(6): error C2780: 'A<T> A(T,T)': expects 2 arguments - 1 provided
<source>(2): note: see declaration of 'A'
https://gcc.godbolt.org/z/6ejfW8G77
这里有两个编译器中的哪一个?
(Clang 在这里没有问题,因为它还不支持带括号的聚合初始化)。
更新:微软承认了这个错误并承诺很快修复: https://developercommunity.visualstudio.com/t/template-argument-deduction-fails-in-case-of-aggre/1467260
【问题讨论】:
-
与 gcc 10.3 相同的错误
-
不完全是,gcc 10.3 甚至无法编译
A a{1,2};,而 msvc 可以
标签: c++ templates language-lawyer c++20