This is the mail archive of the 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]
Other format: [Raw text]

g++ 3.1.1: Implicit-conversion vs. [ ] operator conflict

I just compiled GNU C++ 3.1.1 under Redhat Linux 7.3, and I have a problem
with the pre-existing code:

Suppose I have a class named CHAR, which implement a C-like char* with
built-in memory management. I would like to have it converted automatically
to const char* (needed by C subroutines, for example strlen, etc.); but I
also want to have a [] operator that check the character index. So, I
implemented it in the following way:

class CHAR
   inline operator const char* () const { return ...; }
   inline char& operator[] (int idx) { ...; }

The use:

CHAR mystring;
mystring[0] = 'a';

In the previous version (g++ 3.1) it compiles OK. But with g++ 3.1.1, it
screams at the last line (mystring[0] = 'a';) with the following message:

sourcecode:line: choosing `char& CHAR::operator[](unsigned int)' over
`operator[]', because worst conversion for the former is better than worst
conversion for the latter

Then it would halt. What error have I made? I suppose that the class's
operator[] should precede the standard const char*'s [] operator (because
that's what I want also). Please point to the C++ standard if I've violated


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