关于对象初始化语义的问题

发布于 2024-11-29 07:35:37 字数 1067 浏览 0 评论 0原文

我一直想知道 C++03 规范中围绕对象初始化的语言,特别是第 8.5 节第 9 段,其中指出,

“如果没有为对象指定初始化程序,并且该对象是(可能是 cv 限定的) )非 POD 类类型(或 数组),该对象应默认初始化;如果对象是 const 限定类型,则底层 类类型应具有用户声明的默认构造函数。否则,如果没有为非静态指定初始值设定项 对象,该对象及其子对象(如果有)具有不确定的初始值;如果该物体或任何 其子对象的类型是 const 限定类型,则该程序格式不正确。”

我要特别注意子句,“否则,如果没有为非静态指定初始值设定项 对象,该对象及其子对象(如果有)具有不确定的初始值”。根据第8.5节第5段,默认初始化的定义分为三种情况:

  1. 如果T是非POD类类型(第 9 条),调用 T 的默认构造函数(初始化为 如果 T 没有可访问的默认构造函数,则格式错误)
  2. 如果 T 是数组类型,则每个元素都默认初始化,
  3. 否则,该对象为零初始化。

据我了解,第 9 段指出,如果我们有一个没有初始值设定项的非 POD 类类型,那么将调用它的默认构造函数。让我感到困惑的是 POD 类类型的情况......从我强调的条款看来,没有提到 POD 类类型需要默认构造函数调用。然而,如果我创建了一个 POD 类类型,例如

struct POD_class
{
    int a;
    int b;
    POD_class() { cout << "Default constructor called" << endl; }
};

int main()
{
    POD_class test;
    return 0;
}

POD_class 的默认构造函数,则在使用 g++ 编译和运行此代码时似乎会调用它。因此,即使 POD_class 没有特定的初始化程序,它似乎仍然是默认初始化的,根据默认初始化定义中的情况#1,因为调用了该类型的默认构造函数。

基于上述场景,我的问题是:对于 POD 类,默认初始化对象是否如第 9 段提到的非静态 POD 类意味着不调用其默认构造函数,或者它根本就不是零初始化的?

I've been wondering about the language in the C++03 specification surrounding object initialization, specifically section 8.5 paragraph 9 which states,

"If no initializer is specified for an object, and the object is of (possibly cv-qualified) non-POD class type (or
array thereof), the object shall be default-initialized; if the object is of const-qualified type, the underlying
class type shall have a user-declared default constructor. Otherwise, if no initializer is specified for a nonstatic
object, the object and its subobjects, if any, have an indeterminate initial value; if the object or any
of its subobjects are of const-qualified type, the program is ill-formed."

I want to pay particular attention to the clause, "Otherwise, if no initializer is specified for a nonstatic
object, the object and its subobjects, if any, have an indeterminate initial value"
. According to section 8.5 paragraph 5, the definition of a default-initialization falls into three cases:

  1. if T is a non-POD class type (clause 9), the default constructor for T is called (and the initialization is
    ill-formed if T has no accessible default constructor)
  2. if T is an array type, each element is default-initialized
  3. otherwise, the object is zero-initialized.

So as I understand it, paragraph 9 is stating that if we have a non-POD class type that does not have an initializer, then it's default constructor would be called. What I'm confused by is what happens in the case of POD-class types ... it seems from the clause I highlighted that there is no mention of a default constructor call being required for POD-class types. Yet if I created a POD-class type like

struct POD_class
{
    int a;
    int b;
    POD_class() { cout << "Default constructor called" << endl; }
};

int main()
{
    POD_class test;
    return 0;
}

the default constructor of POD_class seems to be called when this code is compiled and run with g++. Therefore, even if POD_class did not have a specific initializer, it seems it was still default-initialized, per case #1 in the definition of default-initialization, because a default constructor for the type was called.

Based on the above scenario, here is my question: For a POD-class, does not default initializing an object as paragraph 9 mentions for non-static POD-classes mean that its default constructor is not called, or that it's simply not zero-initialized?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疯了 2024-12-06 07:35:37

您的 POD_class 实际上不是 POD 类。 POD 类不能有用户声明的构造函数。

Your POD_class is in fact not a POD class. A POD class cannot have a user-declared constructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文