IanPudney
IanPudney

Reputation: 6021

Equality comparison for Input iterators

For input iterators, what are the requirements for comparing equality if one of the iterators has been invalidated?

input_iter x = foo();
input_iter y = x;
++x;
return x == y;  // What must this return?

In the above example, dereferencing y would obviously be undefined, but is the result of an equality comparison like this defined? Reading cppreference.com, it's definitely the case that two input iterators must compare true if they actually are the same, since input iterators must satisfy EqualityComparable, but I don't actually see anything that says what the result must be if the are not the same. Am I allowed to always return true from operator== (except when comparing against the end iterator)?

Upvotes: 2

Views: 290

Answers (1)

Robᵩ
Robᵩ

Reputation: 168616

"For input iterators, what are the requirements for comparing equality if one of the iterators has been invalidated?"

There are none. Quoting ISO/IEC 14882:2003(E), [lib.input.iterators],

== is an equivalence relation over its domain, (emphasis added)
bool(a==b) != bool(a!=b) over the domain of == (emphasis added)

And,

any copies of the previous value of r [prior to ++r] are no longer ... in the domain of ==.

There is no requirement that == or != have any particular behavior with respect to invalidated input iterators.

Upvotes: 3

Related Questions