The C++ standard says (8.5/5):
To default-initialize an object of type
T
means:
If
T
is a non-POD class type (clause 9), the default constructor forT
is called (and the initialization is ill-formed ifT
has no accessible default constructor).If
T
is an array type, each element is default-initialized.Otherwise, the object is zero-initialized.
With this code
struct Int { int i; };
int main()
{
Int a;
}
the object a
is default-initialized, but clearly a.i
is not necessarily equal to 0 . Doesn't that contradict the standard, as Int
is POD and is not an array ?
Edit Changed from class
to struct
so that Int
is a POD.
From 8.5.9 of the 2003 standard:
The class you show is a POD, so the highlighted part applies, and your object will not be initialized at all (so section 8.5/5, which you quote, does not apply at all).
Edit: As per your comment, here the quote from section 8.5/5 of the final working draft of the current standard (I don't have the real standard, but the FDIS is supposedly very close):
No, the object
a
is not default-initialized. If you want to default-initialize it, you have to say:Your variable is not initialized. Use
to initialize your POD or declare a standard constructor to make it non POD; But you can also use your POD uninitialized for performance reasons like: