I'm trying to figure out what Value initialization is in C++11 so I read this link: https://en.cppreference.com/w/cpp/language/value_initialization.
So here is one of the rules:
- if T is a class type with no default constructor or with a user-provided or deleted default constructor, the object is default-initialized;
...
I can't understand the rule very well.
I wrote a simple example:
struct Test {
int i;
Test() = delete;
double d;
};
int main()
{
Test t{};
std::cout << t.i << std::endl;
std::cout << t.d << std::endl;
return 0;
}
As you see, I've deleted the default constructor, so Test t{};
should be default-initialized, meaning that t.i
and t.d
should be indeterminate values, right? However, both of them are 0
. Ofc I knew that indeterminate value means any value is possible, but I still believe that it is zero-initialized, instead of default-initialized. Because after changing Test() = delete;
into Test() = default;
, the values of i
and of d
are two random numbers, intead of two 0
.
So, a class with a deleted default constructor is initialized by default-initialized or by zero-initialized?
Aucun commentaire:
Enregistrer un commentaire