Question

My question is related to Prasoon's question about non POD types and value initialization.

I tried the following code on online compilers like Ideone and Codepad but the executables gave runtime error on both the sites.

#include <iostream>
#include <cassert>

struct Struct {
    std::string String;
    int Int;
    bool k;
};

struct InStruct:Struct
{
   InStruct():Struct(){}
};

int main()
{
   InStruct i;
   assert ( i.Int == 0);
   std::cout << "Hello";
}

Ideone Output here
Codepad Output here

Does that mean neither of them support C++03 value initialization feature?

Was it helpful?

Solution

Does that mean neither of them support C++03 value initialization feature?

Yes.

Prior to version 4.4, GCC did not completely support value initialization (the Boost GCC compatibility header explains this and has links to the relevant GCC defect reports; see line 77).

If your code needs to be portable, you should be very careful relying on value initialization; GCC did not support it fully until recently and Visual C++ does not fully support it even in its latest version, Visual C++ 2010.

OTHER TIPS

The declaration

InStruct i; 

does not invoke value initialization

$8.5.3/10 - "An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized."

If you want to value-initialize, you would require an expression something like

assert(InStruct().Int == 0);

Try it now! - Ideone supports GCC-4.5.1

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top