Title
reverse_iterator::operator[] calls const version of current[]
Status
nad
Section
[reverse.iter.elem]
Submitter
Timo Bingmann

Created on 2013-11-11.00:00:00 last changed 124 months ago

Messages

Date: 2014-02-14.07:07:05

Proposed resolution:

This wording is relative to N3797.

  1. Edit [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).

Date: 2014-02-13.00:00:00

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

Date: 2014-01-12.14:31:41

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.

History
Date User Action Args
2014-02-14 07:07:05adminsetmessages: + msg6858
2014-02-14 07:07:05adminsetstatus: new -> nad
2014-01-12 14:27:30adminsetmessages: + msg6781
2013-11-11 00:00:00admincreate