title: Why can I make an anonymous union even though the destructor is deleted?
body: <p>From C++ Primer (13.1.6):</p>
  
  <blockquote>
    <p>The compiler will not let us
    define variables or create temporaries of a type that has a deleted destructor.
    Moreover, <strong>we cannot define variables or temporaries of a class that has a member
    whose type has a deleted destructor.</strong> If a member has a deleted destructor, then that
    member cannot be destroyed. If a member can’t be destroyed, the object as a whole
    can’t be destroyed.</p>
  </blockquote>
  
  <p>This certaintly seems true:</p>
  
  <pre><code>class B{
  
  public:
      B() { }
      ~B() = delete;
  
  };
  
  class A{
  
  public:
      A() {}
      ~A() {}
      B b;
  
  };
  
  
  int main(){
  
      //B b; //error
      A a; //error
  
  }
  </code></pre>
  
  <p>brings in all sorts of errors about <code>B</code>'s deleted destructor. This must be because a destructor, even an explicit one, implictly calls destructors of the class members as pointed out in (12.4/8) of N3337:</p>
  
  <blockquote>
    <p>After executing the body of the destructor and destroying any automatic objects allocated within the body, a
    destructor for class X calls the destructors for X’s direct non-variant non-static data members, the destructors
    for X’s direct base classes and, if X is the type of the most derived class (12.6.2), its destructor calls the
    destructors for X’s virtual base classes. </p>
  </blockquote>
  
  <p>I'm now curious why the following works though:</p>
  
  <pre><code>#include &lt;string&gt;
  
  class A{
  public:
      A() { }
      union {
          std::string test;
      };
      ~A() { }
  };
  
  
  int main(){
  
      A b;
  
  }
  </code></pre>
  
  <p>Given that, from 9.5/2 of N3337:</p>
  
  <blockquote>
    <p>If any non-static data member of a union has a non-trivial default
    constructor (12.1), copy constructor (12.8), move constructor (12.8), copy assignment operator (12.8), move assignment operator (12.8), or destructor (12.4), the corresponding member function of the union must be
    user-provided or it will be implicitly deleted (8.4.3) for the union.</p>
  </blockquote>
  
  <p>My class <code>A</code> has an anonymous union object which in turn has a <code>string</code> data member which of course defines its own destructor. So the union, from above, must have a deleted destructor! But my quote from the top says it must not be possible to create objects of <code>A</code>'s type because it has a member with a deleted destructor. If I comment out <code>~A(){}</code> then indeed I get an error, but for some reason my explicit definition is okay. What happens when my explicit definition tries to implicitly destroy the union object?</p>