【问题标题】:Template argument deduction for parenthesized initialization of aggregates in C++C++ 中聚合的带括号初始化的模板参数推导
【发布时间】:2021-12-25 21:32:00
【问题描述】:

在下面的代码中,有一个 A<T> 对象的初始化,模板参数推导使用两种不同的大括号类型:

template<typename T>
struct A{ T x; };

int main() {
    static_assert( A{1}.x == 1 ); //#1: ok in GCC and MSVC
    static_assert( A(1).x == 1 ); //#2: ok in GCC only
}

GCC 和 MSVC 都接受第一种方式,而第二种方式仅在 MSVC 打印错误时适用于 GCC:

error C2641: cannot deduce template arguments for 'A'
error C2780: 'A<T> A(void)': expects 0 arguments - 1 provided
error C2784: 'A<T> A(A<T>)': could not deduce template argument for 'A<T>' from 'int'

演示:https://gcc.godbolt.org/z/97G1acqPr

这是 MSVC 中的错误吗?

【问题讨论】:

  • 不是 100% 肯定,但似乎 MSVC 在这里错了。如果您添加构造函数,两者都可以工作:constexpr A(T v= T{}) : x(v){}

标签: c++ language-lawyer c++20 aggregate-initialization


【解决方案1】:

这是 MSVC 中的一个错误。

以下论文均在C++20中引入:

虽然 MSVC 将它们全部列出为在其 Microsoft C/C++ language conformance by Visual Studio version 页面中实现,但似乎它们已单独正确实现它们

// OK (P0960R3, P1975R0)
struct A { int x; };
A a(1);

// OK (P2131R0)
template<typename T>
struct B { T x; };
B b{1};

// rejects-invalid (allowed by the union of the papers)
template<typename T>
struct C { T x; };
C c(1);

MSVC 似乎错过了实施论文联合。但是,我还没有找到打开的错误报告。

【讨论】:

【解决方案2】:

这些格式良好的行依赖于aggregate deduction candidate,它为T 提供了一种从聚合初始化列表中推导出来的方法。此功能与语法无关,因此其中一个失败而另一个失败已经是不一致的。

在 MSVC 的情况下,是类模板参数推导和带括号的聚合初始化的组合导致了问题(后者单独工作正常)。 MSVC 还有fails to compile A a(1);,更明显是格式良好的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-15
    • 2019-04-13
    • 2016-07-14
    • 2013-09-25
    • 2018-07-13
    • 2021-04-03
    • 1970-01-01
    相关资源
    最近更新 更多