Zach Saw
Zach Saw

Reputation: 4378

C++ pod initialization via default c'tor

Consider this POD:

struct T
{
   int i;
   char c;
};

In which C++ standard was the requirement of POD members be initialized to zero via default c'tor introduced (or was it in the standards from the beginning)?

Yes, that means without user specified c'tor, 'i' and 'c' will both be initialized to 0. See http://msdn.microsoft.com/en-us/library/80ks028k%28VS.80%29.aspx

Upvotes: 4

Views: 542

Answers (2)

Jerry Coffin
Jerry Coffin

Reputation: 490208

What you're talking about is properly called "value initialization". It was introduced in C++03 (it's defined at §8.5/5, in case you want to look at the details).

Upvotes: 3

Prasoon Saurav
Prasoon Saurav

Reputation: 92864

I don't know if I have understood your question properly or not.

that means without user specified c'tor, 'i' and 'c' will both be initialized to 0.

Not necessarily.

For example:

T x; // `i` and `c` are uninitialized

T *ptr = new T; // `i` and `c` are uninitialized
T *pptr = new T(); //`i` and `c` are zero initialized as `T()` implies value initialization

T x(); // x is a function returning a type T and taking no arguments.

To be precise value initialization(C++03 Section $8.5/5) is something you are looking for. It was introduced in C++03.

Upvotes: 5

Related Questions