Vector exception safety

A

Aarti

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.

Regards
 
M

Michael DOUBEZ

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.

But since we erase from begin() to end(), there wouldn't be any
assignement or copy. I wouldn't expect clear() it to throw.

Where did you read it ?

Michael
 
A

Aarti

Aarti a écrit :




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.

But since we erase from begin() to end(), there wouldn't be any
assignement or copy. I wouldn't expect clear() it to throw.

Where did you read it ?

Michael

Hmm. Strostup memtions it in Exception safety of STLs
 
J

James Kanze

Aarti a écrit :
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top