Reading cppreference on value initialization, I have come to this:
1) if T is a class type with no default constructor or with a user-provided or deleted default constructor, the object is default-initialized;
And example:
struct T3
{
int mem1;
std::string mem2;
T3() { } // user-provided default constructor
};
Reading article on default initialization
if T is a non-POD (until C++11) class type, the constructors are considered and subjected to overload resolution against the empty argument list. The constructor selected (which is one of the default constructors) is called to provide the initial value for the new object;
if T is an array type, every element of the array is default-initialized;
otherwise, nothing is done: the objects with automatic storage duration (and their subobjects) are initialized to indeterminate values.
This applies to the examle, T is class type, which means the overload resolution should select the candidate to inicialize the values ( the user-provided default constructor ), but it is empty, so mem1
should stay with inderermined values (thats true ) but same should be mem2
, but that is "default initialized" to "", why is that? Does it work recursively? Every member of T that is class type is subjected to first rule?
I'm quite confused right now.
2)if T is a class type with a default constructor that is neither user-provided nor deleted (that is, it may be a class with an implicitly-defined or defaulted default constructor), the object is zero-initialized and then it is default-initialized if it has a non-trivial default constructor;
And example:
struct T1
{
int mem1;
std::string mem2;
}; // implicit default constructor
the mem1
is zero-initialized to 0, however what does "non-trivial" default contructor means? mem2
is also default-initialized to "", howevever i am still unsure, what does "non-trivial default constructor" means? The default constructor should be generated by compiler, however how does it decide what is and what is not non-trivial --- if non-trivial default constructor means that it has to initialize objects -- same question as above, does it mean that every object is initialized with default constructor?
Aucun commentaire:
Enregistrer un commentaire