The following C++ code fails to compile. As far as I have looked into this problem, I understand that the problem is because the default constructor of the union has been deleted by the compiler. The online note says the following:
If a union contains a non-static data member with a non-trivial default constructor, the default constructor of the union is deleted by default unless a variant member of the union has a default member initializer.
struct A {
int val;
A() : val(0) {}
};
union B
{
A a;
};
B b;
Why is the default constructor of struct A considered non-trivial? How do I work around this problem to make this code compile successfully?