[LLVMbugs] [Bug 17883] New: reverse_iterator::operator[] calls const operator[]

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Mon Nov 11 13:16:48 PST 2013


            Bug ID: 17883
           Summary: reverse_iterator::operator[] calls const operator[]
           Product: libc++
           Version: unspecified
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: All Bugs
          Assignee: hhinnant at apple.com
          Reporter: timo at tbingmann.de
                CC: llvmbugs at cs.uiuc.edu
    Classification: Unclassified

Created attachment 11517
  --> http://llvm.org/bugs/attachment.cgi?id=11517&action=edit

We have a larger library (http://stxxl.sourceforge.net) 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


You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20131111/c44e0a59/attachment.html>

More information about the llvm-bugs mailing list