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


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

Strange operator non-const vs const selection...


I noticed this change a few months ago and the latest C++ spec seems to
be ambiguous on the matter... and the egcs compiler (out of CVS for the
last few months) seems to take the odd approach.

Take the following code:

#include <stdio.h>
#include <iostream.h>

class YFred
{
   public:
      YFred() {}
      operator const char*() const { return "called operator const
char*()"; }
      operator char*() { return "called operator char*()"; }
}

void main()
{
   YFred obFred;
   cout << (const char*)obFred << '\n';
   cout << (const char*)((const YFred &)obFred) << '\n';
}

The first cast will choose the char* cast while the second selects the
const char* cast. Now functions aren't differentiated on return type but
with casting operators the return type is the function. I'm calling
operator const char* precisely.

It seems that egcs is selecting the casting operator primarily on
whether it was called off a const/non-const object and is using the
const'ness of the function as part of the matching. If const'ness of the
function mattered at all in the match then I should be able to do the
following and get separate function signatures:

      operator const char*() const;
      operator const char*();

Which of course doesn't work.

Anyways, it is a rather annoying behavior and, unless someone can point
out where in the C++ spec it specifies that is the way it is work, I'd
recommend egcs change it.

Brian Macy

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