Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ pod initialization via default c'tor

Tags:

c++

standards

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

like image 911
Zach Saw Avatar asked Dec 28 '22 04:12

Zach Saw


1 Answers

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.

like image 62
Prasoon Saurav Avatar answered Dec 30 '22 17:12

Prasoon Saurav