c++ - Problem with casts and conditional expressions
- Adder (36/36) Oct 23 2005 Hello,
Hello,
Here is a stripped down version of a C++ code that fails to compile:
int main ()
{
double *a = reinterpret_cast <double *>
(
1 == 2
?
reinterpret_cast <float *> (0)
:
reinterpret_cast <float *> (0)
);
return 0;
}
The error message is something like this:
main.cpp(10) : Error: need explicit cast to convert
from: float *
to : double *
This looks like the error message given when the third expression in the
conditional expression (the 'e3' in 'e1?e2:e3') does not match the type
of the second expression (the 'e2'):
1 == 2 ? 3 : "Hello";
Here is what I was really trying to compile (demonstrating that this code
might actually be useful for someone):
const xchar *a = reinterpret_cast <const xchar *>
(
sizeof (xchar) == 1
?
reinterpret_cast <const void *> ( "Message")
:
reinterpret_cast <const void *> (L"Message")
);
where xchar is typedef'ed to either char or wchar_t.
It works with Borland and Visual...
Thank you for your time (and this otherwise great compiler),
[Adder]
Oct 23 2005








Adder <Adder_member pathlink.com>