A Base class technique for exception handling
<p>The task of <a href="http://www.gotw.ca/gotw/008.htm" rel="nofollow">GotW#8</a> is to implement an exception-neutral generic stack data structure in C++, assuming only the template argument's destructor doesn't throw. The trick is to handle potentially throwing template argument operations (constructor, copy constructor, assignment) in a way to leave the stack in a consistent state if they throw.</p>

<p>In the solution, Herb Sutter says</p>

<blockquote>
  <p>To keep this solution simpler, I've decided not to demonstrate the base class technique for exception-safe resource ownership.</p>
</blockquote>

<p>After some googling, I found <a href="https://groups.google.com/d/msg/comp.lang.c++.moderated/ajsM_rFWJ74/4iTRyU8LoDUJ" rel="nofollow">this answer</a> by Dave Abrahams dating back to 1997. In his solution he handles allocation and deletion of the memory in the base class, and implements the stack operations in the subclass. This way, he ensures that element copying in the copy constructor is separated from memory allocation, so that if the copying fails, the base class destructor is called, no matter what.</p>

<p>For reference, here is Dave's copy constructor with my comment added:</p>

<pre><code>// v_ refers to the internal array storing the stack elements
Stack(const Stack&amp; rhs)
        : StackBase&lt;T&gt;( rhs.Count() ) // constructor allocates enough space
                                      // destructor calls delete[] appropriately
{
        while ( Count() &lt; rhs.Count() )
           Push( rhs.v_[ Count() ] ); // may throw
}
</code></pre>

<p>If the base constructor succeeds, the memory clean-up in base destructor is guaranteed even if the subclass's copy constructor throws.</p>

<p>My questions are:</p>

<ol>
<li>Is there any other benefit to the approach, except as outlined above?</li>
<li><p>I came up with this copy-constructor when I solved the problem on my own:</p>

<pre><code>// v_ refers to the internal array storing the stack elements
// vsize_ is the amount of space allocated in v_
// vused_ is the amount of space used so far in v_
Stack (const Stack &amp;rhs) :
        vsize_ (0), vused_ (0), v_ (0) {
    Stack temp (rhs.vused_); // constructor calls `new T[num_elements]`
                             // destructor calls `delete[] v_`
    std::copy (rhs.v_, rhs.v_ + rhs.vused_, temp.v_); // may throw
    swap (temp);
}
void swap (Stack &amp;rhs) {
    std::swap (v_, rhs.v_);
    std::swap (vused_, rhs.vused_);
    std::swap (vsize_, rhs.vsize_);
}
</code></pre>

<p>I find it somewhat cumbersome to have a base class when compared to this approach. Is there any reason the base-class technique should be preferred over this temp-copy-then-swap approach? Note that both Dave and I already have the <code>swap()</code> member because we use it in our <code>operator=()</code>.</p></li>
<li>Dave Abrahams' technique doesn't seem to be very well known (according to Google). Does it have a different name, is it standard practice, have I missed something?</li>
</ol>

<p>Notes:</p>

<ul>
<li>assume Dave's <code>Push()</code> in a loop to be equivalent to my usage of <code>std::copy</code></li>
<li>let's keep smart pointers out of the answer, as their usage would take away the point of managing memory explicitly in this exercise</li>
</ul>
