I was refreshing my understanding of value-initialisation versus default-initialisation, and came across this:
struct C { int x; int y; C () { } }; int main () { C c = C (); } Apparently this is UB because
In the case of C(), there is a constructor that is capable of initializing the x and y members, so no initialization takes place. Attempting to copy C() to c therefore results in undefined behavior.
I think I understand why, but I'm not certain. Can someone please elaborate?
Does that mean this is also UB?
int x; x = x; Incidentally, with regards to value initialisation, is the following guaranteed to be zero?
int x = int ();
C c = C();is valid. The uninitialised values of the members ofC()are used to initialisec, and using those values gives undefined behaviour; therefore it is not valid.C c = C();"uses" the uninitialized values of the membersxandyof the temporary created byC(). So it is UB.