Re: STL (boost) and copies of iterator
On 3 Mai, 15:55, Thomas Mang <noth...@provided.com> wrote:
Following questions were raised during the implementation of an iterator
class:
a) Are std-algorithms allowed to store copies of iterators provided as
parameters beyond the call (e.g. in static objects)?
I think it is not forbidden.
I would say, that an arbitrary algorithm function is *not* allowed
to store copies of an arbitrary iterator argument (or functor
argument) for any time *after* the end of the current function
call. My argumentation bases on two views from different
angles:
Let's assume for a second that a function would be allowed to
store iterator values in a static variable: What were the valid
operations that could be done with these stored values?
IMO *maximally* they only allowed operations are: destruction
of these copies and to replacement these copies (e.g. via
assignment) at any future call.
(So there is no real win situation for the algorithm/function to
do so, is it?)
But this upper limit is not enough: We also have to consider a
*lower* limit, which would lead to the conclusion that even
destruction and assignment at an arbitrary late time are not
guaranteed to be allowed: Consider an iterator, which stores a
reference to it's actual container (such an implementation can
be written and thereby fulfilling *all* the iterator requirements).
The destructor (and maybe the copy assignment operator) of
this iterator might use the container reference somehow, which
could be an invalid operation, if the container does not exist
anymore.
The short answer is: They cannot be stored in an static variable,
because they cannot know the lifetime of the backing sequence.
If the last point could be generally solved, then the first point,
too (but with no real economic advantages).
b) Moving from std-world to the real-world (QoI), does anyone know an
algorithm (maybe also including those in boost) where this would make
any sense, or an implementation (including those providing tight error
checking) where it is indeed done?
No.
As an example:
myIterator it1, it2; // 2 instances of myIterator
std::copy(it1, it2, someOutputIterator); // or any other algorithm
// How many instances of myIterator do now exist? 2, or maybe more???
I don't think that a number different from 2 would be valid at any
point *after* the function call.
Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]