Roland Pibinger wrote:[color=blue]
> On 18 Apr 2006 17:26:14 -0700, "Axter" <google@axter.com> wrote:[color=green]
>> Roland Pibinger wrote:[color=darkred]
>>> On Tue, 18 Apr 2006 11:38:37 +0800, Timothee Groleau
>>> <kde@timothee_NO_SPAM_groleau.com> wrote:
>>>> I did some research and I also found this ptr_vector template:
>>>>
http://www.codeproject.com/vcpp/stl/ptr_vecto.asp
>>>> which looks like it may do just what I need. Has anybody tried successfully
>>>> a similar approach before?
>>> To the best of my knowledge this is the only STL-compliant container
>>> for pointers available online.[/color]
>> ptr_vector is not an STL-compliant container.[/color]
>
> It is. What should be not STL-compliant? ptr_vector fulfills the
> requirements of a STL container and the interators work with
> std::algorithms.
>[color=green]
>> Moreover, boost has this container avaiable, and I would recommend
>> using the boost version instread of the CodeProject version, since it's
>> more likely to have better peer review.[/color]
>
> Have you reviewed it? Moreover, the boost pointer containers are not
> primarily containers but object managers that own and 'clone' objects.
> They have totally different semantics compared to my solution.
>[color=green][color=darkred]
>>>> You can find other solutions vendor-specific as well as "smart" containers that
>>> intermingle container functionality and object lifetime management. But if you
>>> look for a pure STL-container for pointers and nothing else then I
>>> know of no altervative.[/color]
>> An STL container of smart pointers, like that in the following link,
>> would be more compatible to STL containers then that of ptr_vector
>> container.
>>
http://axter.com/smartptr[/color]
>
> "Smart" pointer interfaces imitate real pointers. Combined with
> std::containers they produce the same questionable result. But, as
> I've said before, everyone can compare ptr_vector to other solutions
> WRT usability and interface consistency and derive his/her own
> conclusions.
>
> Best wishes,
> Roland Pibinger[/color]
Looking at your ptr_vector implementation, I think one of your examples
is not exception-safe:
void myFunc()
{
ptr_vector<string> ptv;
ptr_vector_owner<string> owner (ptv);
ptv.push_back (new string ("Once upon a time ...")); // LOOK HERE!!!
// ...
if (something.isWrong())
throw exception ("something real wrong!");
// ...
return;
} // pointed-to objects in ptv are deleted here!
Look at the line I labeled "LOOK HERE!!!". push_back can throw an
exception, at which point the string you allocated with new never gets
deleted. To safely push_back a pointer to a dynamically created object
you'd have to do something like:
auto_ptr<string> p = new string ("Once upon a time ...")
ptv.push_back (p.get());
p.release();
Alan