overloading resolution: funny warnings.

Dirk Herrmann dirk@ida.ing.tu-bs.de
Wed Sep 10 02:57:00 GMT 1997


Hello!

I'm not sure, whether I misunderstand things completely, but I believe
there is something strange going on with overloading resolution here.

Compiling old code with egcs-970907 I get the following warnings:

string.hh:33: warning: choosing `String::operator const char *() const'
over `String::operator char *()'
string.hh:33: warning:   for conversion from `const String' to `char *'
string.hh:33: warning:   because conversion sequence for `this' argument
is better
string.hh:33: warning: choosing `String::operator const char *() const'
over `String::operator char *()'
string.hh:33: warning:   for conversion from `const String' to `char *'
string.hh:33: warning:   because conversion sequence for `this' argument
is better

The (I hope sufficient) extract from the code is
(line 33 is marked with '!'):

class String {
  public:
    ...

!   bool operator ==(const String& cmp) const { return *this == cmp.s; }
    bool operator ==(const char* cmp) const { return strcmp(s, cmp) == 0; }

    ...

    operator const char* () const { return s; }
    operator char* () { return s; }

    ...

  private:
    char* s;
};


First, my assumptions (please excuse, if I'm mixing up terms like
conversion and promotion and similar):
- The code in line 33 compares a 'const String' with a 'char *'.
- From the text of the first line of the warning I must conclude: the
  chosen conversion converts the left part of the '==' ('const String') 
  to 'const char *' in order to obtain a comparison of 'const char *'
  with 'char *'.
- The second line of the warning indicates instead, that a conversion
  from `const String' to `char *' is what the compiler wants to
  achieve. In this case, an additional const_cast to remove constness
  was required to get a comparison of a 'char *' with another 'char *'.


Now, my questions, if everything above was correct:

There is an operator== for a comparison of a 'const String' with a
'const char *'. This does only require one single conversion to add
constness. Why doesn't the compiler choose this variant?

Why is the warning issued twice?

Thanks for your time!


Best regards, Dirk Herrmann
----------------------------------------------------------------------
Beat the following spammer sites at their own game! SEND SPAM MAIL TO:
postmaster@agis.net postmaster@ascella.net         ... to be continued




More information about the Gcc-bugs mailing list