I was wondering, how is equality (==) established for STL iterators? Is it a simple pointer comparison (and thus based on addresses) or something more fancy?
If I have t
I was wondering, how is equality (==) established for STL iterators?
Not all iterators can be compared (e.g. Output Iterators are not required to provide op==
). You can use the operator==
when the concept of a range is well-defined for the iterator category under consideration.
Is it a simple pointer comparison (and thus based on addresses) or something more fancy?
Iterators are always implemented with pointers. Edit: I say implemented with -- which refers not to a Standard requirement but rather to the practice of using poitners as the underlying construct. Implementations (like VS) may have special validation checks inserted though.
If I have two iterators from two different list objects and I compare them, will the result always be false?
You are invoking Undefined Behavior.
What about if I compare a valid value with one that's out of range? Is that always false?
Again, you will be invoking UB. The only valid comparison are between two iterators in the same range or between one in the range and another to one past the last element. Note, you can only compare against the iterator to one-past the last element, dereferencing the same leads to UB.