|
Boost : |
From: Anthony Williams (anthwil_at_[hidden])
Date: 2002-09-27 08:34:28
Fernando Cacciola (hotmail) writes:
> void push_back ( Polygon* poly, PolygonInterface* intf )
> {
> // Exceptions can be thrown below...
> mpoly_.reserve( mpoly_.size() + 1);
> interfaces_.reserve(interfaces_.size()+1);
>
> // But no exceptions can be thrown after this point.
> mpoly_ .push_back(poly);
> interfaces_.push_back(intf);
> }
>
> w.r.t to the containers (assuming that the transfers are "basically
> exception safe"),
> does the technique of reserving storage always provide the basic guarantee?
> Is the problem generic enough?
> Is the solution good enough?
container.push_back can always throw an exception, if the copy constructor of
the element to push_back throws an exception.
reserving first ensures that it won't throw due to running out of memory in
the container, but if the copy constructor can throw you will have to pop_back
the first container if the insert into the second throws an exception, so
reserving first doesn't gain you much.
Anthony
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk