Reputation: 2316
So incrementing or decrementing the end() iterator is defined in the standard? On linux, the begin() is implemented as end()++.
#include <list>
#include <iostream>
int main()
{
std::list<int> numbers;
for (int i = 0; i < 10; i++)
numbers.push_back(i);
auto it = numbers.begin();
int count = 3;
while (count)
{
std::cout << *it++;
if (it == numbers.end())
{
++it; // is this ok ???
--count;
std::cout << '\n';
}
}
}
So the output always the same on every platform?
Output:
0123456789
0123456789
0123456789
Upvotes: 6
Views: 1848
Reputation: 153820
Incrementing the iterator returned from end()
of any of the standard C++ library containers results in undefined behavior. Due to an implementation detail common to most implementations of std::list<T>
it may work to increment list.end()
but there is no guarantee that it does.
Upvotes: 9
Reputation: 181745
No, this is not ok. The std::list
iterator is a BidirectionalIterator, which is a refinement of ForwardIterator. The precondition for both ++i
and i++
for a ForwardIterator states:
i
is dereferenceable
which does not hold for end()
as it points past the last item of the list.
Upvotes: 4