Topic: std::map.find() and exceptions


Author: kuyper@wizard.net (James Kuyper)
Date: Fri, 7 Feb 2003 20:01:28 +0000 (UTC)
Raw View
tuneke@web.de (Hilmar Tuneke) wrote in message news:<3E401DF6.3060306@web.de>...
> Hello everybody!
>
> Does the standard say anything about the circumstances that allow
> std::map.find() to throw an exception? There is no throw-clause for this
> method, so it might throw any exception any time. Is this only because
> the elements might throw an exception, or because the method itself
> might throw one?

17.4.4.8p3: "... Any other functions defined in the C++ Standard
Library that do not have an exception-specification may throw
implementation-defined exceptions unless otherwise specified. ...".

However, I think that this permission does not remove the requirements
placed by the standard on the behavior of a function. When the
behavior of std::map.find() is well-defined, throwing an exception
would not count as a correct implementation of that defined behavior.
Therefore, it may only throw an exception if something is wrong.

It will probably throw if the comparison function throws, or if a copy
constructor or copy assignment throws, or if the allocation of
temporary objects needed internally by find() throws. It could also
throw if copy construction or copy assignment of a key value fails to
produce a copy that compares equal with the original, or if the
comparison function is not correctly implementing a consistent strict
weak ordering of the key values.

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: kanze@gabi-soft.de (James Kanze)
Date: Fri, 7 Feb 2003 20:00:59 +0000 (UTC)
Raw View
tuneke@web.de (Hilmar Tuneke) wrote in message
news:<3E401DF6.3060306@web.de>...

> Does the standard say anything about the circumstances that allow
> std::map.find() to throw an exception? There is no throw-clause for
> this method, so it might throw any exception any time. Is this only
> because the elements might throw an exception, or because the method
> itself might throw one?

   18.4.4.8/3:

    No destructor operation defined in the C++ Standard Library will
    throw an exception.  Any other functions defined in the C++ Standard
    Library that do not have an exception-specification may throw
    implementation-defined exceptions unless otherwise specified.  An
    implementation may strengthen this implicit exception-specification
    by adding an explicit one.

Thus, std::map.find can throw an implementation-defined exception.  It
might throw a double every fifth call, for example.  (Note that anything
that is implementation-defined must be documented by the
implementation.)

In practice, of course, QoI also plays a role, and I would feel rather
confident that std::map::find wouldn't throw an exception unless my
comparison function throws.  This is rather intuitive, but I don't
expect std::map::find to copy anything, and I don't expect it to throw
exceptions on its own, just for the fun of it.

--
James Kanze                           mailto:jkanze@caicheuvreux.com
Conseils en informatique orient   e objet/
                    Beratung in objektorientierter Datenverarbeitung

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: tuneke@web.de (Hilmar Tuneke)
Date: Thu, 6 Feb 2003 00:54:04 +0000 (UTC)
Raw View
Hello everybody!

Does the standard say anything about the circumstances that allow
std::map.find() to throw an exception? There is no throw-clause for this
method, so it might throw any exception any time. Is this only because
the elements might throw an exception, or because the method itself
might throw one?


Regards,

Hilmar

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]