<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - reverse_iterator::operator[] calls const operator[]"
   href="http://llvm.org/bugs/show_bug.cgi?id=17883">17883</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>reverse_iterator::operator[] calls const operator[]
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libc++
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>All Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>hhinnant@apple.com
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>timo@tbingmann.de
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=11517" name="attach_11517" title="reverse_iterator_bug">attachment 11517</a> <a href="attachment.cgi?id=11517&action=edit" title="reverse_iterator_bug">[details]</a></span>
reverse_iterator_bug

We have a larger library (<a href="http://stxxl.sourceforge.net">http://stxxl.sourceforge.net</a>) that constructs reverse
iterators using std::reverse_iterator from normal forward random access
iterator classes.

Compiling with libc++ r194397 yields an error similar to:

/software/libcxx-debug/include/c++/v1/iterator:568:17: error: binding of
reference to type 'value_type' (aka 'double') to a value of type
      'const value_type' (aka 'const double') drops qualifiers
        {return current[-__n-1];}
                ^~~~~~~~~~~~~~~

Our iterator implements both mutable operator[] and const operator[]. Const
returns a const value_type&.

The current implementation of
"std::reverse_iterator<>::operator[] const"
makes the operation "current[]" call the const operator[], because the method
is const. The problem is that the "operator[] const" returning a const
value_type&, which cannot be cast back to value_type&.

Comparing libc++

reference operator[](difference_type __n) const {return current[-__n-1];}

against the gcc implementation

reference operator[](difference_type __n) const { return *(*this + __n); }

makes the problem more clear. The gcc implementation copies the iterator first.

Attached is a bad excuse of an iterator example, which implements both
operator[]s. It compiles with gcc-4.6, but not with clang-3.3

Cheers,
Timo</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>