Why is this simple assignment undefined behaviour?
-
26-04-2021 - |
Question
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 ();
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow