Topic: STL exceptions


Author: "Wil Evers" <Wil_Evers@nospam.doosys.com>
Date: 1999/02/19
Raw View
James Kuyper <kuyper@wizard.net> wrote in article
<36C72A12.2C5F7024@wizard.net>...

> The description of the map<> template itself contains no mention of
> throwing exceptions, but is constrained by the general container
> requirements. For instance, for all STL containers, insert() is required
> to have no effects if it throws an exception; the container (along with
> everything else) is either not touched, or returned to it's original
> state.

Does that imply that, say, an implementation of vector<T> is not allowed to
implement an insert somewhere in the middle of the vector by pushing back
the last element and then using the T's assignment operator to move the
intermediate elements up?

After all, if T's assigment operator throws, there is no guarantee we can
succesfully use that same assigment operator to restore the old situation.

It seems to me that the only way to satisfy the requirement you mention is
to copy-construct all of the vector's elements to a newly allocated buffer,
which would (on average) double the running time of the insert operation.

- Wil

--
Wil Evers, DOOSYS IT Consultants, Maarssen, Holland
Note: sender's address is spam-protected -
please remove 'nospam.' when sending email
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Matt Austern <austern@sgi.com>
Date: 1999/02/19
Raw View
"Wil Evers" <Wil_Evers@nospam.doosys.com> writes:

> James Kuyper <kuyper@wizard.net> wrote in article
> <36C72A12.2C5F7024@wizard.net>...
>
> > The description of the map<> template itself contains no mention of
> > throwing exceptions, but is constrained by the general container
> > requirements. For instance, for all STL containers, insert() is required
> > to have no effects if it throws an exception; the container (along with
> > everything else) is either not touched, or returned to it's original
> > state.
>
> Does that imply that, say, an implementation of vector<T> is not allowed to
> implement an insert somewhere in the middle of the vector by pushing back
> the last element and then using the T's assignment operator to move the
> intermediate elements up?

No.  The requirement on the standard is slightly more narrow than
that.  The actual wording (23.1, paragraph 10) is "Unless otherwise
specified ...  if an exception is thrown by an insert() function while
inserting a single element, that function has no effects."

The "unless otherwise specified" caveat refers to sections 23.2.1.3,
paragraph 2, and 23.2.4.3, paragraph 1.  That is, insert into the
middle of a deque<T> or vector<T> does not have to leave the
container's state unchanged if T's copy constructor or assignment
operator throws an exception.  This is for exactly the reason pointed
out above: the obvious and efficient of insert into the middle of a
vector or deque does involve copying.




[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: James Kuyper <kuyper@wizard.net>
Date: 1999/02/15
Raw View
Rekha Raghu wrote:
>
> Does STL classes in general throw exceptions?
>
> For example, insert() method for any container such as "map".

Any function in the C++ standard library which has a "Throws:" clause,
can only throw the specified exceptions, under the specified
circumstances. In addition, there are other, more general restrictions
on exception throwing. However, if there is no such clause, and none of
those restrictions apply, the exception list is completely
implementation-defined. Implementors are encouraged to use exception
classes derived from the standard exception classes.

Most of the templated functions, and member functions of class
templates, either can throw an exception if the template argument is a
type that can throw an exception, or are prohibited from being
instantiated with such arguments. For instance, the standard algorithms
which take a template parameter ForwardIterator may not be instantiated
using a ForwardIterator class which throws exceptions from the
"increment, assignment, comparison or dereference of valid iterators".

The standard does prohibit C++ library classes from having destructors
which throw.

The description of the map<> template itself contains no mention of
throwing exceptions, but is constrained by the general container
requirements. For instance, for all STL containers, insert() is required
to have no effects if it throws an exception; the container (along with
everything else) is either not touched, or returned to it's original
state.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Dave Abrahams" <abrahams@motu.com>
Date: 1999/02/10
Raw View

Rekha Raghu wrote in message <36C0534E.CB976DD1@email.mot.com>...
>Does STL classes in general throw exceptions?


In general, yes.

>For example, insert() method for any container such as "map".

And specifically in that example, yes.




[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Rekha Raghu <crr037@email.mot.com>
Date: 1999/02/10
Raw View
Does STL classes in general throw exceptions?

For example, insert() method for any container such as "map".

Thanks
Rekha
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]