2

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?

Community
  • 1
  • 1

3 Answers3

4

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.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
0

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);
Chubsdad
  • 24,777
  • 4
  • 73
  • 129
  • 2
    `InStruct` has a default constructor. – James McNellis Nov 08 '10 at 04:56
  • @James McNellis: But the base class 'Struct' does not (which as 'Int') – Chubsdad Nov 08 '10 at 04:58
  • @Chubsdad : The compiler synthesized default constructor of `Struct` performs `zero initialization` in this case. – Prasoon Saurav Nov 08 '10 at 04:59
  • @Prasoon Saurav: Is there some reference for this 'zero initialization' which is being done in the case? – Chubsdad Nov 08 '10 at 05:37
  • @Chubsdad: Yes, it's the list of initialization forms in 8.5. `InStruct i` invokes the default constructor of `InStruct`, `Struct()` value initializes the base class, and value initialization for PODs is the same as zero initialization. – James McNellis Nov 08 '10 at 05:46
  • @James McNellis: So why do we say that value initialization is not supported? Also would like to know that since Struct has 'std::string' type, is Struct a POD? – Chubsdad Nov 08 '10 at 06:01
  • 1
    @Chubsdad: Sorry, I missed a step. `Struct()` value initializes the `Struct` base class subobject. Value initialization for a class type with only trivial constructors causes value initialization of each of the class type's member variables, so `String`, `Int`, and `k` are each value initialized (these are absolutely terrible variable names...). `String` ends up getting its default constructor invoked and `Int` and `k` are each zero-initialized. – James McNellis Nov 08 '10 at 06:03
  • Value initialization is not fully supported because if it were, the member variables of the `Struct` base class subobject should be initialized but they are not. – James McNellis Nov 08 '10 at 06:04
  • 1
    @Chubsdad: `..is Struct a POD?` No it is not. – Prasoon Saurav Nov 08 '10 at 06:09
  • 1
    @Chubsdad: No problem, but you can't thank me for the downvote; you'll have to thank whoever downvoted it :-) – James McNellis Nov 08 '10 at 06:19
0

Try it now! - Ideone supports GCC-4.5.1

kuszi
  • 2,069
  • 29
  • 36