gpt4 book ai didi

c++ - 为什么这个格式错误的程序在 g++ 中编译得很好?

转载 作者:行者123 更新时间:2023-11-30 01:48:00 25 4
gpt4 key购买 nike

<分区>

我在 here 上阅读有关 C++ 中的默认初始化的内容.它说:

If T is a const-qualified type, it must be a class type with a user-provided default constructor.

该链接上给出的示例是(我只显示了与我的问题相关的程序语句,其他我省略了):

struct T1 {};
int main()
{
const T1 nd; // error: const class type with implicit ctor
}

但它在 gcc 4.8.1 和 4.9.2 上编译良好。我还使用 -std=c++14 选项编译了它,但它仍然可以正常编译。这是 gcc 扩展还是其他?

所以,我认为上述程序成功编译的原因是struct T1 中没有成员。因此,在这种情况下,此处不会发生默认初始化。但是如果我像这样添加一个数据成员:

struct T1 { int a; };
int main()
{
const T1 nd; // error: const class type with implicit ctor
}

然后编译器给出相应的错误信息如下:

6 11 [Error] uninitialized const 'a' [-fpermissive]
2 8 [Note] 'const struct T1' has no user-provided default constructor
3 8 [Note] and the implicitly-defined constructor does not initialize 'int T1::a'

那么,语句不应该这样写吗?

If T is a const-qualified type having at least one data member, it must be a class type with a user-provided default constructor.

如果我错了并且理解不正确,请纠正我。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com