关于对象初始化语义的问题
我一直想知道 C++03 规范中围绕对象初始化的语言,特别是第 8.5 节第 9 段,其中指出,
“如果没有为对象指定初始化程序,并且该对象是(可能是 cv 限定的) )非 POD 类类型(或 数组),该对象应默认初始化;如果对象是 const 限定类型,则底层 类类型应具有用户声明的默认构造函数。否则,如果没有为非静态指定初始值设定项 对象,该对象及其子对象(如果有)具有不确定的初始值;如果该物体或任何 其子对象的类型是 const 限定类型,则该程序格式不正确。”
我要特别注意子句,“否则,如果没有为非静态指定初始值设定项 对象,该对象及其子对象(如果有)具有不确定的初始值”。根据第8.5节第5段,默认初始化的定义分为三种情况:
- 如果T是非POD类类型(第 9 条),调用 T 的默认构造函数(初始化为 如果 T 没有可访问的默认构造函数,则格式错误)
- 如果 T 是数组类型,则每个元素都默认初始化,
- 否则,该对象为零初始化。
据我了解,第 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:
- 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) - if T is an array type, each element is default-initialized
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
POD_class
实际上不是 POD 类。 POD 类不能有用户声明的构造函数。Your
POD_class
is in fact not a POD class. A POD class cannot have a user-declared constructor.