Following code is in simplest form:
struct X {
operator char () const { return 'a'; }
};
int main ()
{
X obj, *p = &obj;
char a = *p; // ok
char c = (true)? *p : 'z';
}
This code gives compiler error as,
error: operands to ?: have different types ‘X’ and ‘char’
Why *p is not resolved to char when there is no ambiguity in class X for typecasting operator ?
Is such spurious error message correct or it’s a g++ bug ?
[Update Note: Interestingly this scenario doesn’t generate such error]
It seems to be a compiler-bug. I checked it out in the spec, the Standard clearly says (§5.16/3 – C++03),
and the rest of the section explains how the conversion is done. There is nothing that stops
*pfrom implicitly converting intochartype, using the user-defined conversion operator.Also, I compiled it with
(GCC) 4.5.0. It gives no error, with-pedanticoption as well. Tried it-std=c++98and-std=c++0x. Still no error.Most definitely, it is a compiler-bug.