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++/4556: g++ reports an error for operator[] related conversions



>Number:         4556
>Category:       c++
>Synopsis:       g++ reports an error for operator[] related conversions
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          rejects-legal
>Submitter-Id:   net
>Arrival-Date:   Fri Oct 12 08:46:00 PDT 2001
>Closed-Date:
>Last-Modified:
>Originator:     Andras Pataki
>Release:        gcc-3.0.1
>Organization:
>Environment:
dbgloss2:~/try$ /usr/gnu/gcc-3.0.1/bin/gcc -v
Reading specs from /usr/gnu/gcc-3.0.1/lib/gcc-lib/sparc-sun-solaris2.6/3.0.1/specs
Configured with: ../gcc-3.0.1/configure --prefix=/usr/gnu/gcc-3.0.1
Thread model: posix
gcc version 3.0.1
dbgloss2:~/try$ uname -a
SunOS dbgloss2 5.6 Generic_105181-23 sun4u sparc SUNW,Ultra-80
dbgloss2:~/try$ 
>Description:
The following simple program fails to compile:

dbgloss2:~/try$ cat 6.cc
class X
{
public:
    char &operator[](unsigned i);
    operator const char *();
};



void f()
{
    X x;
    int i = 0;
    char c = x[i];
}

dbgloss2:~/try$ /usr/gnu/gcc-3.0.1/bin/g++ -c 6.cc
6.cc: In function `void f()':
6.cc:14: choosing `char& X::operator[](unsigned int)' over `operator[]'
6.cc:14:   because worst conversion for the former is better than worst 
   conversion for the latter
dbgloss2:~/try$ echo $?
1
dbgloss2:~/try$ 

For the x[i] expression's interpretation the two options are:
  char &X::operator[](unsigned i)
     requiring an integral conversion for 'i' (int -> unsigned)
  built in operator[] for char *
     requiring a user defined conversion for X -> const char *
Since the latter requires a user defined conversion, it is less preferable than the former.
So I believe the compiler should pick the former and not return an error message and exit with an exit status 1.
>How-To-Repeat:
See description
>Fix:
The workaround it to declare 'i' as an 'int' not as an 'unsigned'.
This is painful for a large codebase though.
>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]