Questions, please

P

Peter Pichler

There is no cast in C or C++ that is
portably guaranteed to just apply a different type to the same
representation

int a = 42;
float b = *((float*)&a); /* :) */

Of course this is not portable, how can you *portably* interpret
a 'float' from an 'int' bit pattern anyway? Applying a different
type to the same representation is a rather dubious requirement,
IMNSHO. (Yes, I know that it was your point.)

Peter
 
D

Dave Thompson

int a = 42;
float b = *((float*)&a); /* :) */
But the cast doesn't do it, the dereference does, by "creating" an
lvalue that designates the object with a different type. As far as the
cast goes, float* is not required to have the same representation as
int* -- although in practice it always does; on machines where data
pointers vary it is character and void pointers that are different.
Of course this is not portable, how can you *portably* interpret
a 'float' from an 'int' bit pattern anyway? Applying a different
type to the same representation is a rather dubious requirement,
IMNSHO. (Yes, I know that it was your point.)
In general yes, although my point was really that even in those cases
where it does make sense C and C++ still don't guarantee it.

Cf Ada, where you have substantial control over some representations,
and extensive inquiries into all of them which can verify that they
are as you expect, so it can be portable to do this, at least to the
extent that it will either work or give a clear error which you might
well be able to recover from; nothing undefined or unpredictable.

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,813
Members
47,357
Latest member
sitele8746

Latest Threads

Top