www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript

c++ - Problem with casts and conditional expressions

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