0

As pointed out in other questions here on SO, equality comparison between two iterators from different containers is UB.

In my case I have two list iterators, which might not be from the same container:

std::list< int >::iterator a, b;

Suppose that I know for sure that none of them is an end iterator, my question is if I am allowed to do:

&(*a) == &(*b)
Community
  • 1
  • 1
sbabbi
  • 11,070
  • 2
  • 29
  • 57
  • Why not? if they are not end-iterators, dereferencing them yields an `lvalue`, whose address you surely can get. – lisyarus Mar 21 '14 at 18:53

1 Answers1

2

Sure, you can do that, as you're not comapring the iterators but the addresses of their referands.

Assuming iterators are from different containers, this comparison cannot ever be true in a well-formed program, though.

jrok
  • 54,456
  • 9
  • 109
  • 141