Boost logo

Boost :

From: Douglas Gregor (dgregor_at_[hidden])
Date: 2005-02-09 19:13:50

On Feb 9, 2005, at 3:36 PM, Chris Uzdavinis wrote:

> Doug Gregor <dgregor_at_[hidden]> writes:
>> No, it is not legal to compare iterators from different
>> containers. Operations that require two iterators mandate that the
>> iterators be part of the operation's "domain". Iterators from
>> different containers can never be in the same domain.
> I generally agree, but think it gets a bit murky when we deal with
> std::list iterators, since they can be spliced back and forth between
> different containers without being invalidated.

Splice essentially transfers ownership of iterations from one container
to another...

> It seems strange that we can compare the two iterators into a list and
> that's valid. Then splice one element into another list--the
> iterators have not changed whatsoever--but now comparing them is
> invalid.

Sure, but I can do this, too:

        vector<int> v1, v2;
        vector<int>::iterator i, j;
        i = v1.begin(); j = v1.end();
        i == j;
        i = v2.begin(); j = v2.end();
        i == j;

The domain changed for both iterators, just like in the list example.
Domains are not static properties, and this is _really_ important. It
shows up in the stream iterators, too, where the specifications of !=
and == make absolutely no sense without careful considering the domain
of the operations.


Boost list run by bdawes at, gregod at, cpdaniel at, john at