I read about value-initialization in http://ift.tt/1imUFJK , but I'm really confused by the sample code below:
struct A {
int i;
A() {} // user-provided ctor, does not initialize i
};
struct B { A a; }; // no user-provided ctor
std::cout << B().a.i << '\n'; // value-initializes a B temporary
// leaves b.a.i uninitialized in C++03
// sets b.a.i to zero in C++11
// (note that B{}.a.i leaves b.a.i uninitialized in C++14, but for
// a different reason: C++14's B{} is aggregate-initialization)
cppreference says:
In all cases, if the empty pair of braces {} is used and T is an aggregate type
that is not a class type with a default constructor (until C++14), aggregate-initialization is performed instead of value-initialization.
I think B{} should be value-initialized in C++ 11 because B has a default constructor(because of A).According to the rules about value-initialization:
2) If T is an non-union class type without any user-provided constructors, then the object is zero-initialized and then the implicitly-declared default constructor is called (unless it's trivial)
(since C++11)(until C++14)
So I think B{} should be zero-initialized first and then calls the default constructor. But unfortunatly, I compile the code with clang++, -std=c++11, the value of B{}.a.i is uninitialized. What's the problem?
Aucun commentaire:
Enregistrer un commentaire