Re: best way to "delete" all objects in a std::vector.
On Jun 5, 9:22 am, Kai-Uwe Bux <jkherci...@gmx.net> wrote:
Jerry Coffin wrote:
In article <daniel_t-4F9851.22361904062...@earthlink.vsrv-
sjc.supernews.net>, danie...@earthlink.net says...
[ ... ]
AFAIK, the implementation I presented is guaranteed by the
standard not to read or copy any pointers after the delete.
I don't see any such guarantee, though I'll admit I might
have missed it.
Maybe you missed it because you snipped it. So, let's look at it:
struct Delete_ptr {
template<class T> T* operator()(T* p) const { delete p; return 0; }
};
...
transform(s.begin(),s.end(),s.begin(),Delete_ptr());
Note:
a) Delete_ptr::operator() returns 0.
b) The proposed solution uses transform and not foreach.
And... Where do you have a guarantee that std::transform
doesn't read through the pointer a second time, after having
called the fucntional object? Or that
std::vector::iterator::operator*() doesn't read the object
before returning an lvalue to it.
I very heavily stressed in my original posting that this was for
nitpickers, because, of course, no implementation will do such
things, and we all know it. But the fact remains that the
standard requires that all objects in a container be copyiable,
at all times (even if no member function is called). And
between the delete in the operator(), above, and the moment
transform assigns the result of the operator to its target,
there is a deleted pointer in the container, which is undefined
behavior.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34