The first example for Boost Pointer Container adds a raw pointer to the structure:
class zoo
{
boost::ptr_vector<animal> the_animals;
public:
void add_animal( animal* a )
{
the_animals.push_back( a );
}
};
But what if push_back
, or any other member function that might trigger reallocation, throws an exception during the process? From my understanding, in that case, it would be up to the caller to manage the memory for the given object, but because the caller passes a raw pointer to a class whose purpose is to manage memory, it's most likely that the caller doesn't.
So, wouldn't one need to use some kind of unique smart pointer in the code example above to wrap the pointer before providing it to the container and to be absolutely sure that no memory leaks? The containers do provide an overload for such smart pointers, but they don't enforce their usage.
Or is the argument that the containers simply won't ever throw exceptions during any add operation and it always succeeds?