成员初始化列表是否没有定义值?
Is a member initialization list without a value defined?
说我有 :
class Foo
{
public:
int x;
Foo() : x() {}
}
构造函数有运行后读x
会不会是UB?更具体地说,这是什么类型的初始化,零初始化、直接初始化还是默认初始化?
我知道如果我们有:
Foo() : x(42) {}
x
会直接初始化为 42
但我不太确定上面的代码片段,如果结果是这样,我不想被 UB 狼咬默认初始化。
what type of initialization is this
x()
执行 value-initialization:
when a non-static data member or a base class is initialized using a member initializer with an empty pair of parentheses or braces (since C++11)
;
作为非class类型int
,x
最终被零初始化为0
。
- otherwise, the object is zero-initialized.
说我有 :
class Foo
{
public:
int x;
Foo() : x() {}
}
构造函数有运行后读x
会不会是UB?更具体地说,这是什么类型的初始化,零初始化、直接初始化还是默认初始化?
我知道如果我们有:
Foo() : x(42) {}
x
会直接初始化为 42
但我不太确定上面的代码片段,如果结果是这样,我不想被 UB 狼咬默认初始化。
what type of initialization is this
x()
执行 value-initialization:
when a non-static data member or a base class is initialized using a member initializer with an empty pair of parentheses
or braces (since C++11)
;
作为非class类型int
,x
最终被零初始化为0
。
- otherwise, the object is zero-initialized.