|
Boost : |
From: JOAQUIN LOPEZ MU?Z (joaquin_at_[hidden])
Date: 2005-02-11 16:28:28
----- Mensaje original -----
De: Daniel James <daniel_at_[hidden]>
Fecha: Viernes, Febrero 11, 2005 10:45 am
Asunto: [boost] Unordered associative containers and swap's exception
safety
> Hello everyone,
>
> I've got an exception safety problem implementing the hash
> containers.
> The draft standard says (from 6.3.1.1):
>
> 'For unordered associative containers, no swap function throws an
> exception unless that exception is thrown by the copy constructor
> or
> copy assignment operator of the container's Hash or [equality]
> Pred
> object (if any).'
>
> This is a problem, because for the container to be valid, its hash
> buckets, equality predicate and hash function need to consistent.
> Since
> both the hash and predicate can throw, this is impossible (or
> hard?) to
> achieve. If swapping/copying the first one succeeds, and the
> second one
> throws they won't match. Any ideas on what I should do?
Hi Daniel. I think little can be done here without penalizing
the normal users (i.e. those providing notrow Hash and Pred objects.)
The solution you propose is probably the only way out, but it
incurs an extra indirection each time one of the objects
is accessed.
FWIW, all implementations I've seen simply assume swapping
of these objects won't throw --and this is the case also for
other STL containers, where a simple recovery strategy would be
nonetheless possible. Having a nonthrowing swap, then it is
trivial to have exception-safe strong assignment.
I think this has received attention at comp.std.c++, but
without any definitive conclusion AFAIK.
So my advice is to just assumme Hash and Pred objects won't
throw when being swapped through std::swap and be happy.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk