Initialization of values โโof type of non-aggregate class is covered by 8.5p8. In your case, the (non-union) class has an implicitly defaulted default constructor with no parameters (12.1p5), which is not deleted and is trivial (ibid). So the second bullet is 8.5p8:
- if T is a (possibly cv-qualified) class of non-union type without a user-supplied or removable default constructor, then the object is initialized to zero and if T has a nontrivial default value, the default initialized constructor;
So, A must be zero initialized, and the program should print 0 .
In the following program:
struct A { int get() { return i; } private: int i; }; #include <iostream> int main() { char c[sizeof(A)]; new (c) int{42}; std::cout << (new (c) A{})->get() << '\n'; }
gcc-4.7.2 correctly outputs 0 ; gcc-4.6.3 displays 42 incorrectly; clang-3.0 goes absolutely crazy and dumps garbage (e.g. 574874232 ).
ecatmur
source share