Is it safe to use std::list as a circular list?

2020-04-06 03:40发布

问题:

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

回答1:

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.



回答2:

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.