Re: More of a raw kind of cast
On Thu, 29 Jun 2006 19:15:52 GMT, Frederick Gotham
<fgothamNO@SPAM.com> wrote in comp.lang.c++:
Before I begin, here's a list of assumptions for this particular example:
(1) unsigned int has no padding bits, and therefore no invalid bit-
patterns or trap representations.
(2) All types have the same alignment requirements.
(3) sizeof(double) >= sizeof(unsigned)
=====================================
We can cast from double to unsigned int as follows:
No, you can't. A cast is ONLY performed by a cast operator, either
the original C cast, or one of the new cast operators defined in C++.
int main()
{
double d = 672.23;
unsigned i = d;
}
The code above performs a conversion, specifically an automatic
conversion on assignment. There is no cast involved. A cast is an
EXPLICIT conversion, defined as such by both C and C++. Some
conversions occur automatically in expressions, others require a cast
operator.
We CAN cast from double to unsigned like this:
int main()
{
double d = 672.23;
unsigned i = (double)d; // other C++ casts could be used
}
This shows that you can use a cast operator to specifically perform a
conversion that would occur automatically in the expression anyway.
However, if we want a raw kind of cast, i.e. simply access the double's
data in memory as if it were an unsigned int, then we can write:
This is not a cast either. What you are doing is trying to access the
raw underlying bits of an object, with an lvalue of a different type.
int main()
{
double d = 672.23;
unsigned i = *reinterpret_cast<unsigned*>( &d );
}
However, my question is this:
Do we have to bother with pointers as in the previous snippet, or can
we use references?
First, the answer is no.
Second, you are not actually having to "bother with pointers", you are
bothering with addresses, which is the name of the type of value held
by a pointer.
The unary '&' operator generates the address of its operand. The cast
takes that address rvalue, which has the type "address of double", and
convert it to an rvalue which has the type "address of unsigned int".
Most likely, there is no change in the bitwise representation of the
address, merely in the number of bits read from memory during the
lvalue to rvalue conversion and how they are interpreted.
No actual pointer exists or is created.
int main()
{
double d = 672.23;
unsigned i = reinterpret_cast<unsigned&>( d );
}
This is trying to cast a double to a reference to int, which quite
impossible.
I don't know what you think the overhead of the pointer cast is. You
are only changing how the address is used to access memory, not
actually create a pointer.
Consider this variation on your second and third examples:
int main()
{
double d1 = 672.23, d2 = 666.89;
unsigned i1 = d1;
unsigned i2 = *reinterpret_cast<unsigned*>(&d2);
}
Do you think the compiler will generate more machine instructions for
the second than it does for the first?
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html