XPl*_*mer 8 c++ initialization
I'm debugging some code that essentially is identical to this:
struct Foo { int a; int b; };
struct Bar { Bar() {} Foo foo{0}; };
Run Code Online (Sandbox Code Playgroud)
When I make an instance of Bar
, it seems like both a
and b
are initialized to zero. Is this guaranteed? Where can I find that in the spec?
如果初始化子句的数量小于成员[和基数 (Since C++17)] 的数量或初始化器列表完全为空,则剩余的成员[和基数 (Since C++17)] 将被初始化 [通过它们的默认成员初始值设定项,如果在类定义中提供,否则 (C++14 起)] 通过空列表,根据通常的列表初始化规则(对非类类型和非聚合类执行值初始化)使用默认构造函数,以及聚合的聚合初始化)。如果引用类型的成员是这些剩余成员之一,则程序格式错误。
Foo
没有默认的成员初始化器 ( int b{0};
),因此b
将通过带有空列表的列表初始化来初始化,这意味着非类类型的值初始化:b = int() // = 0
。