struct B
{
B() : member{}{};
int member[10];
};
int main()
{
B b;
}
VS2013编译器会发出以下警告:
警告 C4351:新行为:数组 'B::member' 的元素将进行默认初始化 1> test.vcxproj -> C:\Users\asaxena2\documents\visual studio 2013\Projects\test\Debug\test.exe
这里有文档here。
使用C++11,并应用“默认初始化”的概念,意味着B.member的元素不会被初始化。
但我认为member{}
应该执行值初始化而不是默认初始化。VS2013编译器有问题吗?
$8.5/6
默认初始化一个类型为T
的对象意味着:
- 如果T
是一个(可能带有cv限定符的)类类型(第9条),则调用T
的默认构造函数(如果T
没有可访问的默认构造函数,则此初始化是不合法的);
- 如果T
是数组类型,则每个元素都会被默认初始化;
- 否则,不执行任何初始化。
如果程序要求对const
限定的类型T
进行默认初始化,则T
必须是具有用户提供的默认构造函数的类类型。
$8.5.1
List-initialization of an object or reference of type
T
is defined as follows:
— If the initializer list has no elements andT
is a class type with a default constructor, the object is value-initialized.
— Otherwise, ifT
is an aggregate, aggregate initialization is performed (8.5.1).If there are fewer initializer-clauses in the list than there are members in the aggregate, then each member not explicitly initialized shall be initialized from an empty initializer list (8.5.4). [ Example:
struct S { int a; const char* b; int c; }; S ss = { 1, "asdf" };
initializes
ss.a
with1
,ss.b
with"asdf"
, andss.c
with the value of an expression of the formint()
, that is,0
. —end example ]