Topic: cast operator


Author: jpotter@falcon.lhup.edu (John Potter)
Date: Tue, 19 Nov 2002 23:34:35 +0000 (UTC)
Raw View
On Tue, 19 Nov 2002 20:49:22 +0000 (UTC),
francesco.fassi.agos@libero.it ("Zaza") wrote:

> I have a question about the use of cast operator in situations like:

> float a = 10;
> (double&) a = 10;
> The compiler accept this code, but is it standard?

Yes.

> If the meaning of (r)t is
> casting a r-value of type t in a r-value of type r then what the previous
> instruction does?

*(double*)&a = 10;

reinterpret_cast<double&>(a) = 10;
Would be prefered in C++ which is still the same as
*reinterpret_cast<double*>(&a) = 10;

It is not nice to store a double in the space of a float destroying
something else.

John

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: gennaro_prota@yahoo.com (Gennaro Prota)
Date: Wed, 20 Nov 2002 19:10:41 +0000 (UTC)
Raw View
On Tue, 19 Nov 2002 23:34:35 +0000 (UTC), jpotter@falcon.lhup.edu
(John Potter) wrote:

>reinterpret_cast<double&>(a) = 10;
>Would be prefered in C++ which is still the same as
>*reinterpret_cast<double*>(&a) = 10;

A few days ago there was a discussion about this on the boost list.
The goal there was to understand whether the boost definition of
addressof is guaranteed to work on all conforming implementations. My
point is that 5.2.10/7 and/or 5.2.10/10 should be slightly changed to
clarify that. In particular, a problem that concerns the case above
too (besides addressof) is that 5.2.10/7 says you can't do anything
portably with the value of reinterpret_cast<> except converting it
back to the original type. So it is far from clear to me what does the
following mean

   5.2.10/10: "a reference cast reinterpret_cast<T&>(x) has the
   same effect as the conversion *reinterpret_cast<T*>(&x) with the
   built-in & and * operators"

There's dereferencing involved. What is "the same effect as"?

PS: for those who are interested to the boost discussion as well, the
initial message is here:

   http://aspn.activestate.com/ASPN/Mail/Message/1414562

Genny.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: gennaro_prota@yahoo.com (Gennaro Prota)
Date: Wed, 20 Nov 2002 19:32:08 +0000 (UTC)
Raw View
>PS: for those who are interested to the boost discussion as well, the
>initial message is here:
>
>   http://aspn.activestate.com/ASPN/Mail/Message/1414562

In fact I've just verified that not all the messages of the thread
appear in the tree at the bottom of the page and those that are
missing don't seem to be reachable with a search either. Sorry for the
wrong link.

Genny.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: jpotter@falcon.lhup.edu (John Potter)
Date: Thu, 21 Nov 2002 17:04:35 +0000 (UTC)
Raw View
On Wed, 20 Nov 2002 19:10:41 +0000 (UTC), gennaro_prota@yahoo.com
(Gennaro Prota) wrote:

> On Tue, 19 Nov 2002 23:34:35 +0000 (UTC), jpotter@falcon.lhup.edu
> (John Potter) wrote:

> >reinterpret_cast<double&>(a) = 10;
> >Would be prefered in C++ which is still the same as
> >*reinterpret_cast<double*>(&a) = 10;

> A few days ago there was a discussion about this on the boost list.
> The goal there was to understand whether the boost definition of
> addressof is guaranteed to work on all conforming implementations.

5.2.10/3 is quite clear in making almost nothing portable.  However,
5.2.10/4-5 is quite clear that the implementation is expected to do
something reasonable.

double d;
char& cr1(reinterpret_cast<char&>(d));
char& cr2(*reinterpret_cast<char*>(
      reinterpret_cast<unsigned long>(&d)));
char c1(cr1);
char c2(cr2);
char c3;
memcpy(&c3, &d, 1);

When the unsigned long is large enough, we can expect each of the
above to be the same.  When it is not large enough, the first should
still be the same as the last.

> My
> point is that 5.2.10/7 and/or 5.2.10/10 should be slightly changed to
> clarify that. In particular, a problem that concerns the case above
> too (besides addressof) is that 5.2.10/7 says you can't do anything
> portably with the value of reinterpret_cast<> except converting it
> back to the original type. So it is far from clear to me what does the
> following mean

>    5.2.10/10: "a reference cast reinterpret_cast<T&>(x) has the
>    same effect as the conversion *reinterpret_cast<T*>(&x) with the
>    built-in & and * operators"

> There's dereferencing involved. What is "the same effect as"?

Dereferencing a pointer produces an lvalue.  Are you thinking about
the lvalue to rvalue conversion that sometimes follows?

double d;
float& fr1(reinterpret_cast<float&>(d));
float& fr2(*reinterpret_cast<float*>(&d));

The same effect, there is no temporary of type double or float
produced because there is no rvalue of those types used.  The
only rvalue is a pointer type.  We have the lvalue to rvalue
with address of and rvalue to lvalue with dereference.  The
first does the same thing without the rvalue.

John

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: francesco.fassi.agos@libero.it ("Zaza")
Date: Tue, 19 Nov 2002 20:49:22 +0000 (UTC)
Raw View
hello,

I have a question about the use of cast operator in situations like:

float a = 10;
(double&) a = 10;
The compiler accept this code, but is it standard? If the meaning of (r)t is
casting a r-value of type t in a r-value of type r then what the previous
instruction does?

thanks.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]