Re: Exception Safety Concerning Range Members
on Sun May 20 2007, "jehugaleahsa-AT-gmail.com" <jehugaleahsa-AT-gmail.com> wrote:
Hello:
I have recently been reading books by both Scott Meyers and Herb
Sutter.
Dr. Meyers points out the considerable efficiency gains by using range
members, such as assign and insert.
Dr. Sutter writes that range members are an exception to the exception
guarantees of the STL.
That's at best a misconception.
The STL gives the basic exception guarantee everywhere, the strong
guarantee where it can without loss of efficiency, and the nothrow
guarantee where it is important to do so (e.g. for recovery or so that
stack unwinding can proceed). The exception guarantees for the range
members are specified entirely consistently in that context. See
http://www.boost.org/more/generic_exception_safety.html
Sutter's characterization of the STL in my copy of "Exceptional C++"
that "all standard containers must also implement the strong guarantee
for all operations (with two exceptions)" puts the emphasis in the
wrong place, and the two exceptions are ultimately incorrectly stated,
as shown below.
He mentioned something about handling such cases would pose an
efficiency cost on the implementations.
In the case where the copy ctor and/or assignment operator of T can
throw an exception, **giving the strong guarantee** for
vector<T>::insert(pos,it0,it1) and deque<T>::insert(pos,it0,it1) would
impose an efficiency cost (you'd essentially have to allocate and copy
an entirely new container).
That doesn't mean the case isn't "handled." It also doesn't apply to
the range constructors of std::vector<>.
Is this true for std::list?
No. In fact, 23.2.3.3 list modifiers says specifically,
template <class InputIterator>
void insert(iterator position , InputIterator first ,
InputIterator last );
...
1 Remarks: Does not affect the validity of iterators and
references. If an exception is thrown there are no effects.
Which means it unconditionally gives the strong guarantee. I suggest
you go to the standard for the official guarantees. The secondary
sources are often wrong or misleading.
Could you create a temporary list and then
splice it into *this?
Yes, you can.
template <typename FITER>
list(FITER first, FITER past)
{
while (first != past)
{
insert(end(), *(first++)); // could be optimized
}
}
template <typename FITER>
void insert(iterator pos, FITER first, FITER past)
{
(*this).splice(pos, std::list<T, allocator_type>(first, past)); // allocators are stateless
// splice is constant time
// and has a strong exception guarantee
}
Is this exception-safe?
Depends what you mean by that ;-). When I say "exception-safe," I
mean "gives the basic guarantee."
Does it require considerable, additional processing? Why don't any
implementations seem to use it?
Did you look at the actual code? If it's not ultimately doing
something equivalent to the above, in the case where the allocators
are equal, it's nonconforming. If you have found such a nonconforming
implementation, could you please let us know which one it is?
Thanks,
--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]