On Jul 20, 11:32 am, Michael DOUBEZ <michael.dou...@free.fr> wrote:
> Aarti a écrit :
> > I was reading about exception safety in vectors and came across a
> > staement that says
> > The clear operation of vector guarantees a nothrow provided the copy
> > and assignment operator do not throw an exception.
> > Now clear just deletes all the elements of a vector. What has copy
> > and assignment operator got to do with it?
> > Can anyone please shed some light.
> My only guess is that clear() is equivalent to erase(begin(),end()) and
> erase() is nothrow provided the copy and assignment operator do not
> throw an exception.
Exactly. For whatever reasons, the standard makes no promess
with regards to clear(), saying simply that clear() is an exact
equivalent of erase(begin(),end()). And so the only guarantees
are those of erase, and there's no special case in erase for the
case where all of the elements are being erased. (In fact, in
std::vector, you could probably make a no throw guarantee for
erase any time the second iterator is end().)
> But since we erase from begin() to end(), there wouldn't be any
> assignement or copy. I wouldn't expect clear() it to throw.
In practice, nor would I.
I suspect that if you asked the committee nicely, specifying the
exact change in the wording, and if they could find time (far
from sure), they'd be willinig to make the change.
--
James Kanze (Gabi Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34