This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of NAD status.

2347. reverse_iterator::operator[] calls const version of current[]

Section: 25.5.1.6 [reverse.iter.elem] Status: NAD Submitter: Timo Bingmann Opened: 2013-11-11 Last modified: 2021-06-06

Priority: 2

View all other issues in [reverse.iter.elem].

View all issues with NAD status.

Discussion:

Currently reverse_iterator::operator[]() returns "current[-n-1]" and has an "unspecified" return type.

When Iterator is a mutable random access iterator, the expression "current[-n-1]" calls "Iterator::operator[] const", which returns a const reference. This const reference cannot be converted back to a mutable reference.

This issue is related to the "unspecified" return value of reverse_iterator::operator[], see defect 386.

The -1 is due to "current" pointing one item beyond the reverse_iterator's real current value.

The current libstdc++ implementation reads "*(current + n)" for reverse_iterator::operator[].

This copied current, advances (backwards) via operator+ and dereferences. It bypasses the issues due to reverse_iterator::operator[] being const by copying the iterator.

[2014-02-13 Issaquah : close as NAD]

Proposed resolution:

This wording is relative to N3797.

  1. Edit 25.5.1.2 [reverse.iterator], class template reverse_iterator synopsis, as indicated:

    namespace std {
      template <class Iterator>
      class reverse_iterator : public
      iterator<typename iterator_traits<Iterator>::iterator_category,
        typename iterator_traits<Iterator>::value_type,
        typename iterator_traits<Iterator>::difference_type,
        typename iterator_traits<Iterator>::pointer,
        typename iterator_traits<Iterator>::reference> {
      public:
        […]
        unspecifiedreference operator[](difference_type n) const;
        […]
      };
      […]
    }
    
  2. Edit [reverse.iter.opindex] as indicated:

    unspecifiedreference operator[](
      typename reverse_iterator<Iterator>::difference_type n) const;
    

    -1- Returns: current[-n-1]*(current + n).