When compiling the following program with the shipped gcc 4.0.1 from XCode 2.2
on an Apple iMac, the result is:

> Mac:~/Desktop pfeil$ ./bugdemo 
> write write write write 
> true

but should be:
> write read write read
> true

Or in other words, the compiler should use the read/const version of the
operator[] when there is no assignment and the lvalue/reference version when
there is an assignment to the reference of the operator.

Or again in other words: How to access the read opperator?

---

// g++ bugdemo.cpp -o bugdemo
#include <iostream>
using namespace std;
class Array {
  bool  data[ 100 ];
public:
  const bool operator[]( const unsigned int index ) const {
    cout << "read ";
    return data[ index ];
  }
  bool &operator[]( const unsigned int index ) {
    cout << "write ";
    return data[ index ];
  }
};
int main( int argc, char **argv ) {
  Array a;
  a[ 0 ] = true;
  a[ 1 ] = a[ 0 ];
  cout << endl << (a[ 0 ]?"true":"false") << endl;
  return 0;
}


-- 
           Summary: Right read operator not accessible after overloading.
           Product: gcc
           Version: 4.0.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: pfl at iis dot fhg dot de
 GCC build triplet: i686-apple-darwin8
  GCC host triplet: i686-apple-darwin8
GCC target triplet: i686-apple-darwin8


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=27356

Reply via email to