Vector exception safety

Discussion in 'C++' started by Aarti, Jul 20, 2007.

  1. Aarti

    Aarti Guest

    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
    Aarti, Jul 20, 2007
    #1
    1. Advertising

  2. 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
    Michael DOUBEZ, Jul 20, 2007
    #2
    1. Advertising

  3. Aarti

    Aarti Guest

    On Jul 20, 2:32 am, Michael DOUBEZ <> 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.
    >
    > 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
    Aarti, Jul 21, 2007
    #3
  4. Aarti

    James Kanze Guest

    On Jul 20, 11:32 am, Michael DOUBEZ <> 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
    James Kanze, Jul 22, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Harrison

    Exception safety in the STL

    John Harrison, Jul 6, 2003, in forum: C++
    Replies:
    5
    Views:
    4,084
    John Harrison
    Jul 8, 2003
  2. pmatos
    Replies:
    6
    Views:
    23,787
  3. Replies:
    8
    Views:
    1,915
    Csaba
    Feb 18, 2006
  4. Javier
    Replies:
    2
    Views:
    561
    James Kanze
    Sep 4, 2007
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    357
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page