Generally speaking C++ doesn't allow comparing iterators between different containers. For example:
int main() {
std::vector<int> v = {1, 2, 3};
std::vector<int> w = {4, 5, 6};
std::cout << v.end() == w.end() << std::endl; // undefined!
}
But is this true also for subspans created with std::span::subspan()
? For example:
int main() {
int a[4] = { 1, 2, 3, 4};
std::span<int> s(a);
std::span<int> t = s.subspan(1, 2);
std::cout << t.begin() - s.begin() << std::endl;
}
This prints 1
, which is expected, because internally iterators are probably just pointer to the underlying array. The question is: does the standard guarantee that this works correctly?
And if so, more generally, can I also compare iterators from any spans that come from the same contiguous object in memory? For example:
int main() {
int a[5] = { 1, 2, 3, 4, 5};
std::span<int> s(a);
std::cout << (s.subspan(1, 1).end() < s.subspan(3, 1).begin()) << std::endl;
}