Re: Exception-safety of erase
on Tue Jul 15 2008, Adam Badura <abadura-AT-o2.pl> wrote:
But this opens another question. This time regarding iterators
(possibly those passed to "erase" ??? so we do not fall off topic
here :)).
In the Standard in point 23.1 $10 in the forth point we can read:
"no copy constructor or assignment operator of a returned iterator
throws an exception.". Fine. But there is no definition of what is
named a "returned iterator".
It's an iterator type that appears as the return type of a standard
library (member) function.
Are those the container's member types "iterator" and
"const_iterator" (and maybe revers iterators as well when they
exist?). It would be nice and logical. But on the other hand why
wouldn't the Standard say just "iterator" (and those other iterators -
maybe a common term could be introduced here) instead of the cryptic
(IMHO) "returned iterator"?
I don't remember exactly, but when Greg Colvin and I wrote that text I
think we were trying to avoid implying that user's iterators couldn't
throw. What about std::reverse_iterator<myiterator> for example?
Note that it is not obvious (again IMHO) that the Standard meant
here any iterators not just those instances returned by container
functions. I don't see any reason to think that the Standard just used
a strange name. And although copy constructor and assignment operator
belong to iterator's type and not particular instance their behavior
naturally depends on that particular instance. And thous the non-
throwing guarantee may depend on the instance as well.
No. And do you know how I know that? It's because that wasn't our
intention when we wrote the text.
I'm not joking; the intention of the authors matters when interpreting
the standard. We can't eliminate every bit of potential ambiguity in
the standard, since it's written in (a form of) english.
And if so, then following code:
container::iterator it1;
container::iterator it2 = it1;
might result in an exception in initialization of i2 since i1 is not
returned from a container (if the "returned iterator" is to be
understood literally).
It depends whether container is a container in the standard. If it is,
the assignment will not throw...
(Actually it might throw exception in default construction of it1
as well since there is no guarantee of non-throwing default
constructor of container's iterator type ??? or is it a general iterator
requirement? Or I again missed something?)
.....but default construction might.
--
Dave Abrahams
BoostPro Computing
http://www.boostpro.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]