Reputation: 42215
struct A
{
private:
int a, b, c;
};
int main()
{
A a1{};
A a2 = {};
return 0;
}
The code was compiled by VC++ 2012 (with the latest update "Nov 2012 CTP").
I expect a1 and a2 are zero-initialized, but not. a1 and a2 are not initialized.
Why?
Upvotes: 5
Views: 178
Reputation: 131789
Note that this answer had a slight rewrite that changed the end result to the opposite of what it was before. Thanks to @David Rodríguez - dribeas for enlightening me. :)
This is a bug. Clang 3.2 trunk and GCC 4.7+ agree too and will zero-initialize the members.
Time for some standardese. Note that T x{};
(or = {}
) can be interpreted as either list-initialization or aggregate initialization. A
here is not an aggregate because it has private members, and as such can not be initialized by the latter.
§8.5.1 [dcl.init.aggr] p1
An aggregate is an array or a class (Clause 9) with [...] no private or protected non-static data members [...]
This only leaves list-initialization and that will value-initialize both a1
and a2
.
§8.5.1 [dcl.init.list] p3
List-initialization of an object or reference of type
T
is defined as follows:
- If the initializer list has no elements and
T
is a class type with a default constructor, the object is value-initialized.
Value-initialization is specified as follows for our specific case:
§8.5 [dcl.init] p7
if
T
is a (possibly cv-qualified) non-union class type without a user-provided constructor, then the object is zero-initialized [...]
And this in turn means that the members should be zeroed out.
Upvotes: 6
Reputation: 473222
You mean Microsoft's Community Tech Preview compiler, which they aren't even confident enough in to call it a beta, has bugs in it? ;)
This is a bug; it should behave as you expect. Please report it as such.
Upvotes: 7