Following code is in simplest form:
struct X {
operator char () const { return \'a\'; }
};
int main ()
{
X obj, *p = &obj;
char a = *p; // ok
char
It seems to be a compiler-bug. I checked it out in the spec, the Standard clearly says (§5.16/3 - C++03),
Otherwise, if the second and third operand have different types, and either has (possibly cv-qualified) class type, an attempt is made to convert each of those operands to the type of the other.
and the rest of the section explains how the conversion is done. There is nothing that stops *p
from implicitly converting into char
type, using the user-defined conversion operator.
Also, I compiled it with (GCC) 4.5.0
. It gives no error, with -pedantic
option as well. Tried it -std=c++98
and -std=c++0x
. Still no error.
Most definitely, it is a compiler-bug.