Consider this code:
#include <iostream>
using namespace std;
struct Z
{
Z() { cout << "ctor Z" << endl; }
};
struct A
{
A() = default;
A(int xx) : x(xx) {}
int x;
Z z;
};
int main()
{
A a;
cout << "a.x = " << a.x << endl;
}
Compiled with newest GCC it prints:
ctor Z
a.x = 0
But compiled with newest Clang it prints:
ctor Z
a.x = 4198800
Why? Can someone explain this? Does GCC initiates the 'x' prinitive value while it doesn't have to do it? Or, maybe Clang doesn't initiante the 'x' with 0, while it should do it?
The same happens when I remove my constructors from the "A" struct, so it is not a matter of the constructor being marked as 'default' or something like that. To put this simply - one compiler generates code which calls constructors of its members being class objects, and also members being primitives. The other compiler runs constructor of only the class objects members.
Why?