Home Dashboard Directory Help

VC++ does not value-initialize members of derived classes without user-declared constructor by Sylvester Hesp


 as Won't Fix Help for as Won't Fix

Sign in
to vote
Type: Bug
ID: 484295
Opened: 8/20/2009 3:11:35 PM
Access Restriction: Public
User(s) can reproduce this bug


When value-initializing an object that hasn't have a user-declared ctor, the standard clearly says that each member and baseclass of that object should be value-initialized.

From ISO/IEC 14882:2003:
An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized.


To value-initialize an object of type T means:
— if T is a class type (clause 9) with a user-declared constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
— if T is a non-union class type without a user-declared constructor, then every non-static data member and base-class component of T is value-initialized;
— if T is an array type, then each element is value-initialized;
— otherwise, the object is zero-initialized

In the example program below (see "Steps to reproduce"), both Foo1 and Foo2 are classes without user-declared constructors. Yet, only the members of Foo2 are actually value-initialized when value-initializing the Foo2 object. The members of Foo1 remain default-initialized (which means uninitialized for basic types like 'int'). But the only difference is that Foo1 has a base class, while Foo2 doesn't. To me there seems to be no reason why the members of Foo1 shouldn't be value-initialized when Foo1 itself is being value-initialized, and the compiler is clearly in violation of the standard on this point.

Not only that, it can seriously break programs when someone decides to give a certain class or struct a base class, thereby removing all value-initialization of their members that the programmer might have been counting on. Additionally, such a bug would be very difficult to track down. And I can't imagine anyone counting on the current behaviour that a value remains uninitialized, especially when it only remains uninitialized when the class has a base class.

This also happens in older VC++ compilers and the Xbox 360 compiler.
Sign in to post a comment.
Posted by TheArtTrooper on 10/13/2011 at 12:09 AM
I reproduced this problem with Visual C++ 11 (part of Visual Studio 11 Developer Preivew). CL.EXE reports version 17.00.40825.2 for 80x86.
Posted by Niels Dekker on 4/25/2010 at 2:57 AM
Note: it looks like this report by Sylvester Hesp is related to a report by Pavel Kuznetsov: "Value-initialization in new-expression", https://connect.microsoft.com/VisualStudio/feedback/details/100744
Posted by Microsoft on 8/25/2009 at 9:55 AM
Hi: I can confirm that this is a bug with Visual C++. Unfortunately it does not meet the triage bar for the current release of Visual C++ - but we will keep the issue in our database and we will look at it again during the development phase of a future release of Visual C++.

Jonathan Caves
Visual C++ Compiler Team
Posted by Microsoft on 8/23/2009 at 9:58 PM
Thanks for your feedback. We are routing this bug to the product unit who works on that specific feature area. The team will review this issue and make a decision on whether they will fix it or not for the next release.

Thank you,
Visual Studio Product Team
Sign in to post a workaround.
Posted by Sylvester Hesp on 8/26/2009 at 2:47 AM
There is no workaround, other than adding a default ctor in which you excplicitely value- or zero-initialize all members, which has the implication that you can no longer default-initialize the members of the object.