This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

c++/3830: compile error, if a class has operator[] and operator char*()



>Number:         3830
>Category:       c++
>Synopsis:       compile error, if a class has operator[] and operator char*()
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          rejects-legal
>Submitter-Id:   net
>Arrival-Date:   Thu Jul 26 05:46:01 PDT 2001
>Closed-Date:
>Last-Modified:
>Originator:     Martin Gerbershagen
>Release:        gcc-3.0
>Organization:
>Environment:

>Description:
When the attached code is compiled with g++ -c the following
error message is issued:

bug.C: In function `void x()':
bug.C:20: choosing `char& A::operator[](unsigned int)' over `operator[]'
bug.C:20:   because worst conversion for the former is better than worst 
   conversion for the latter
bug.C:20: choosing `char& A::operator[](unsigned int)' over `operator[]'
bug.C:20:   because worst conversion for the former is better than worst 
   conversion for the latter

This error is wrong, the code is legal code.
>How-To-Repeat:
class A {
   char *p;
public:
   char operator[](unsigned int i) const {
      return p[i];
   }
   char& operator[](unsigned int i) {
      return p[i];
   }
   operator char*() {
      return p;
   }
   operator const char*() const {
      return p;
   }
};

void x() {
   A a;
   char c = a[1];
}
>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]