title: How to handle constructors that must acquire multiple resources in an exception safe manner
body: <p>I've got a non-trivial type that <em>owns</em> multiple resources.  How do I construct it in an exception safe manner?</p>
  
  <p>For example, here is a demo class <code>X</code> that holds an array of <code>A</code>:</p>
  
  <pre><code>#include "A.h"
  
  class X
  {
      unsigned size_ = 0;
      A* data_ = nullptr;
  
  public:
      ~X()
      {
          for (auto p = data_; p &lt; data_ + size_; ++p)
              p-&gt;~A();
          ::operator delete(data_);
      }
  
      X() = default;
      // ...
  };
  </code></pre>
  
  <p>Now the obvious answer for this particular class is to <strong>use <code>std::vector&lt;A&gt;</code></strong>.  And that's good advice.  But <code>X</code> is only a stand-in for more complicated scenarios where <code>X</code> must own multiple resources and it isn't convenient to use the good advice of "use the std::lib."  I've chosen to communicate the question with this data structure simply because it is familiar.</p>
  
  <p>To be <em>crystal</em> clear:  If you can design your <code>X</code> such that a defaulted <code>~X()</code> properly cleans everything up ("the rule of zero"), or if <code>~X()</code> only has to release a single resource, then that is best.  However, there are times in real life when <code>~X()</code> has to deal with multiple resources, and this question addresses those circumstances.</p>
  
  <p>So this type already has a good destructor, and a good default constructor.  My question centers on a non-trivial constructor that takes two <code>A</code>'s, allocates space for them, and constructs them:</p>
  
  <pre><code>X::X(const A&amp; x, const A&amp; y)
      : size_{2}
      , data_{static_cast&lt;A*&gt;(::operator new (size_*sizeof(A)))}
  {
      ::new(data_) A{x};
      ::new(data_ + 1) A{y};
  }
  </code></pre>
  
  <p>I have a fully instrumented test class <code>A</code> and if no exceptions are thrown from this constructor it works perfectly well.  For example with this test driver:</p>
  
  <pre><code>int
  main()
  {
      A a1{1}, a2{2};
      try
      {
          std::cout &lt;&lt; "Begin\n";
          X x{a1, a2};
          std::cout &lt;&lt; "End\n";
      }
      catch (...)
      {
          std::cout &lt;&lt; "Exceptional End\n";
      }
  }
  </code></pre>
  
  <p>The output is:</p>
  
  <pre><code>A(int state): 1
  A(int state): 2
  Begin
  A(A const&amp; a): 1
  A(A const&amp; a): 2
  End
  ~A(1)
  ~A(2)
  ~A(2)
  ~A(1)
  </code></pre>
  
  <p>I have 4 constructions, and 4 destructions, and each destruction has a matching constructor.  All is well.</p>
  
  <p>However if the copy constructor of <code>A{2}</code> throws an exception, I get this output:</p>
  
  <pre><code>A(int state): 1
  A(int state): 2
  Begin
  A(A const&amp; a): 1
  Exceptional End
  ~A(2)
  ~A(1)
  </code></pre>
  
  <p>Now I have 3 constructions but only 2 destructions.  The <code>A</code> resulting from <code>A(A const&amp; a): 1</code> has been <strong>leaked!</strong></p>
  
  <p>One way to solve this problem is to lace the constructor with <code>try/catch</code>.  However this approach is not scalable.  After every single resource allocation, I need yet another nested <code>try/catch</code> to test the next resource allocation and deallocate what has already been allocated.  Holds nose:</p>
  
  <pre><code>X(const A&amp; x, const A&amp; y)
      : size_{2}
      , data_{static_cast&lt;A*&gt;(::operator new (size_*sizeof(A)))}
  {
      try
      {
          ::new(data_) A{x};
          try
          {
              ::new(data_ + 1) A{y};
          }
          catch (...)
          {
              data_-&gt;~A();
              throw;
          }
      }
      catch (...)
      {
          ::operator delete(data_);
          throw;
      }
  }
  </code></pre>
  
  <p>This correctly outputs:</p>
  
  <pre><code>A(int state): 1
  A(int state): 2
  Begin
  A(A const&amp; a): 1
  ~A(1)
  Exceptional End
  ~A(2)
  ~A(1)
  </code></pre>
  
  <p>But this is <em>ugly!</em>  What if there are 4 resources?  Or <em>400?!</em>  What if the number of resources is <em>not known at compile time?!</em></p>
  
  <p>Is there a <em>better</em> way?</p>