gpt4 book ai didi

c++ - clang 与 gcc 中空结构的默认初始化程序

转载 作者:搜寻专家 更新时间:2023-10-31 00:55:21 25 4
gpt4 key购买 nike

以下代码使用 GCC 的某些版本和 Clang 的某些版本进行编译(请参阅下面的版本)。

struct my_struct {};

int main(int argc, char** argv) {
const my_struct my_object;
return 0;
};

编译:g++ clang_error.cppclang++ clang_error.cpp。我在 4.8.4 有 g++,在 3.6.0 有 clang++。

错误信息是:

clang_error.cpp:7:19: error: default initialization of an object of const type 'const my_struct' without a user-provided default constructor
const my_struct my_object;
^
clang_error.cpp:7:28: note: add an explicit initializer to initialize 'my_object'
const my_struct my_object;
^
= {}
1 error generated.

受影响的版本

使用编译器资源管理器 here ,我可以看到高达 4.5.4 的 GCC 受到影响。 Clang 影响到 3.9.0。

问题

我的问题是:C++ 标准对此有何规定?理想情况下,我会关心 C++14,但我并不挑剔。

上面的示例代码是否符合标准?

到目前为止我发现了什么

我在 Draft N3797 中找到了以下内容C++14 的。

§ 7.1.6.1 The cv-qualifiers [dcl.type.cv]
2 [ Note: Declaring a variable const can affect its linkage (7.1.1) and its usability in constant expressions (5.19). As described in 8.5, the definition of an object or subobject of const-qualified type must specify an initializer or be subject to default-initialization. — end note ]

§ 8.5
7 To default-initialize an object of type T means:
— if T is a (possibly cv-qualified) class type (Clause 9), the default constructor (12.1) for T is called (and the initialization is ill-formed if T has no default constructor or overload resolution (13.3) results in an ambiguity or in a function that is deleted or inaccessible from the context of the initialization);
— if T is an array type, each element is default-initialized;
— otherwise, no initialization is performed.

最佳答案

What does the C++ standard say about this?

Is the above example code standard-conformant?

在 C++14 中,它是不一致的:

n4431 (2015) standard draft [dcl.init] / 7:

If a program calls for the default initialization of an object of a const-qualified typeT,Tshall be a class typewith a user-provided default constructor.

my_struct 没有用户提供的默认构造函数,因此您不应进行默认初始化。

然而,一些较新的编译器似乎选择放宽规则,可能是因为它受到缺陷报告的影响:DR 253 .


即将发布的标准改变了措辞:

Current (2017) standard draft [dcl.init] / 7:

A class type T is const-default-constructible if default-initialization of T would invoke a user-provided constructor of T (not inherited from a base class) or if

(7.4) each direct non-variant non-static data member M of T has a default member initializer or, if M is of class type X (or array thereof), X is const-default-constructible,

(7.5) if T is a union with at least one non-static data member, exactly one variant member has a default member initializer,

(7.6) if T is not a union, for each anonymous union member with at least one non-static data member (if any), exactly one non-static data member has a default member initializer, and

(7.7) each potentially constructed base class of T is const-default-constructible.

If a program calls for the default-initialization of an object of a const-qualified type T, T shall be a const-default-constructible class type or array thereof.

措辞对我来说有点模棱两可,但我认为因为 my_struct 没有违反 7.4 的成员(并且不是 union ,所以 7.5 不适用,没有 union 成员所以 7.6不适用且没有基础,因此 7.7 不适用),它是 const-default-constructible,因此该示例将是一致的。

关于c++ - clang 与 gcc 中空结构的默认初始化程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42251142/

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