Value initialization of nested structs does not work properly
I have this struct definitions:
struct inner
{
int i;
std::string str;
};
struct outer
{
inner member[32];
};
Now I want to create a value-initialized instance of outer
, so I write
outer o = {};
With GCC, this works just fine: all inner.i
are zeroed and all inner.str
are empty. But on VS2013, only the inner.str
are empty; all inner.i
contain garbage ie are not properly initialized.
Without the std::string
member, the zero-initialization of inner.i
works with VS2013.
What does the standard say on this? I always assumed {}
initializes everything, either by zeroing or by calling the default constructor. Am I wrong or is this a very bad bug in VS2013?
This is simply aggregate initialization:
If the number of initializer clauses is less than the number of members or initializer list is completely empty, the remaining members are initialized by their default initializers, if provided in the class definition, and otherwise (since C++14) by empty lists, which performs value-initialization.
Value initialization means that every element of member
is value-initialized, which in turn means that i
should be zero.
So yes, this is a VS2013 bug.
链接地址: http://www.djcxy.com/p/67792.html下一篇: 嵌套结构的值初始化不能正常工作