title: C++ Default constructors in union with variant member with non-trivial default constructor
body: <p>I recently read a description of default constructors for unions:
  <a href="http://en.cppreference.com/w/cpp/language/default_constructor" rel="nofollow noreferrer">Default Constructor</a></p>
  
  <p>There is a following rule: </p>
  
  <blockquote>
    <p>Blockquote
    Deleted implicitly-declared default constructor:
    [...] T is a union with at least one variant member with non-trivial default constructor, and no variant member of T has a default member initializer.[...]</p>
  </blockquote>
  
  <p>Then I decided to make an exercise and verify the rule.</p>
  
  <pre><code>struct Member {
   public:
    // Member() : mX(0) {}
    virtual int GetX() {
      return mX;
    }
    int mX;
  };
  
  union DefaultConstructor {
    int mA;
    Member mMember;
    int GetA();
  };
  </code></pre>
  
  <p>With use of gcc v5.3.1 (I know it is pretty old) I receive expected error:</p>
  
  <pre><code>&gt; ../src/DefaultConstrcutors.cpp: In function ‘void
  &gt; Test_DefaultConstructors()’: ../src/DefaultConstrcutors.cpp:26:22:
  &gt; error: use of deleted function
  &gt; ‘DefaultConstructor::DefaultConstructor()’    DefaultConstructor foo;
  &gt;                       ^ In file included from ../src/DefaultConstrcutors.cpp:19:0:
  &gt; ../src/DefaultConstructors.h:155:7: note:
  &gt; ‘DefaultConstructor::DefaultConstructor()’ is implicitly deleted
  &gt; because the default definition would be ill-formed:  union
  &gt; DefaultConstructor {
  &gt;        ^ ../src/DefaultConstructors.h:157:10: error: union member ‘DefaultConstructor::mMember’ with non-trivial ‘Member::Member()’   
  &gt; Member mMember;
  &gt;           ^
  </code></pre>
  
  <p>Ok, so according to rule, if I provide a default member initializer for a variant member, then it should compile. So I've changed union definition to:</p>
  
  <pre><code>union DefaultConstructor {
        int mA = 0;
        Member mMember;
        int GetA();
      };
  </code></pre>
  
  <p>And now it should compile, but I've received the same error.</p>
  
  <p>Next step was to provide default initializer for a mMember instead of mA (only one union variant member may have default initializer).</p>
  
  <pre><code>union DefaultConstructor {
        int mA;
        Member mMember{};
        int GetA();
      };
  </code></pre>
  
  <p>Now it compiles. </p>
  
  <p>The question is: why it didn't compile in second case, when mA had default initializer? According to mentioned rule it should be possible. What more similar rule is provided here: <a href="http://en.cppreference.com/w/cpp/language/union" rel="nofollow noreferrer">Union declaration</a></p>
  
  <blockquote>
    <p>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 .</p>
  </blockquote>
  
  <p>Anyone has an idea why doesn't it work?</p>
  
  <p>Greetings,
  Piotr</p>