title: Destruct a Reference Member Initialized from Factory Method
body: <p>To initialize a reference member without a reference to an external entity passed to the constructor of the containing class, a factory method can be used, like this:</p>
  
  <pre><code>struct B
  {
      B() : a(new_a()) {}
      // factory
      a&amp; new_a()
      {
          A* a = new A;
          return *a;
      }
      A&amp; a;
  };
  </code></pre>
  
  <p>However, of course <code>B::a</code> won't be destroyed when <code>B</code>'s lifetime ends, because it is a reference. But there is not access to <code>B::a</code> outside of <code>B</code>. So this is a memory leak. To fix this one can call <code>A::~A()</code> from <code>B::~B()</code></p>
  
  <pre><code>B::~B() {a.~A();}
  </code></pre>
  
  <p>But I have read manually calling destructors was a no-no, so putting a call to <code>A::~A()</code> in the destructor of <code>B::~B()</code> falls flat, or does it?</p>
  
  <p>Is there cleaner solution?</p>