This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the EGCS project.
Strange operator non-const vs const selection...
- To: egcs-bugs@egcs.cygnus.com
- Subject: Strange operator non-const vs const selection...
- From: Brian Macy <bmacy@sunshinecomputing.com>
- Date: Tue, 27 Jul 1999 09:15:49 -0700
- CC: strobert@strobe.net, "Richard J. Rauenzahn" <rrauenza@cup.hp.com>, cstaley@calpoly.edu
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