Re: How to make this exception-safe
On 19 Nov, 22:28, Yechezkel Mett <ymett.on.use...@gmail.com> wrote:
On Nov 18, 8:43 pm, Triple-DES <DenPlettf...@gmail.com> wrote:
[snip]
How would you go about making an exception-safe version of V::V()?
This is what I could come up with:
// 1st attempt
#include <memory>
V::V() {
v_.reserve(3);
std::auto_ptr<C> c1( new C(2) );
std::auto_ptr<C> c2( new C(1) );
std::auto_ptr<C> c3( new C(3) );
v_.push_back( c1.release() );
v_.push_back( c2.release() );
v_.push_back( c3.release() );
}
Which is tedious if you are inserting more objects. Does anyone have a
better solution?
PS! boost/tr1 shared_ptr or similar can not be used.
Why not? (It's important to know what constraints we are working
under.)
I would suggest using a container which holds pointers and manages
lifetimes -- boost has a few under the name Pointer Container.
Sorry, it is essential that it is written entirely in Standard C+
+(03).
Otherwise:
namespace
{
std::auto_ptr<C> new_C(int n) { return std::auto_ptr<C>(n); }
}
V::V() {
std::auto_ptr<C> a[] = {
new_C(2),
new_C(1),
new_C(3)
};
const size_t count = sizeof a / sizeof a[0]
v_.reserve(count);
std::transform(a, a + count, std::back_inserter(v_),
boost::bind(&std::auto_ptr<C>::release, _1));
}
This looks like a solid solution. I would of course have to use the
standard binders.
[I took the liberty of snipping the try/catch example, since it is
worse than the other two, IMHO]
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]