Idiomatic use of auto_ptr to transfer ownership to a container
<p>I'm refreshing my C++ knowledge after not having used it in anger for a number of years.  In writing some code to implement some data structure for practice, I wanted to make sure that my code was exception safe.  So I've tried to use <code>std::auto_ptr</code>s in what I think is an appropriate way.  Simplifying somewhat, this is what I have:</p>

<pre><code>class Tree
{
public:
    ~Tree() { /* delete all Node*s in the tree */ }
    void insert(const string&amp; to_insert);
    ...

private:
    struct Node {
        ...
        vector&lt;Node*&gt; m_children;
    };

    Node* m_root;
};

template&lt;T&gt;
void push_back(vector&lt;T*&gt;&amp; v, auto_ptr&lt;T&gt; x)
{
    v.push_back(x.get());
    x.release();
}

void Tree::insert(const string&amp; to_insert)
{
    Node* n = ...;  // find where to insert the new node
    ...
    push_back(n-&gt;m_children, auto_ptr&lt;Node&gt;(new Node(to_insert));
    ...
}
</code></pre>

<p>So I'm wrapping the function that would put the pointer into the container, <code>vector::push_back</code>, and relying on the by-value <code>auto_ptr</code> argument to
ensure that the <code>Node*</code> is deleted if the <code>vector</code> resize fails.</p>

<p>Is this an idiomatic use of <code>auto_ptr</code> to save a bit of boilerplate in my
<code>Tree::insert</code>?  Any improvements you can suggest?  Otherwise I'd have to have
something like:</p>

<pre><code>Node* n = ...;  // find where to insert the new node
auto_ptr&lt;Node&gt; new_node(new Node(to_insert));
n-&gt;m_children.push_back(new_node.get());
new_node.release();
</code></pre>

<p>which kind of clutters up what would have been a single line of code if I wasn't
worrying about exception safety and a memory leak.</p>

<p>(Actually I was wondering if I could post my whole code sample (about 300 lines) and ask people to critique it for idiomatic C++ usage in general, but I'm not sure whether that kind of question is appropriate on stackoverflow.)</p>
