Topic: reinterpret_cast


Author: Kin Chan <firstian@usa1.com>
Date: 1996/06/17
Raw View
I have a question about the usage of reinterpret_cast as described in the
draft standard. It mentions that it is only possible to use
reinterpret_cast to cast a pointer into an integral type and vice versa.
Does that mean it will not cast an integral type to another integral
type? What about pointer to pointer?

Thanks

Kin Chan
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1996/06/19
Raw View
In article 7677@usa1.com, Kin Chan <firstian@usa1.com> writes:
>I have a question about the usage of reinterpret_cast as described in the
>draft standard. It mentions that it is only possible to use
>reinterpret_cast to cast a pointer into an integral type and vice versa.
>Does that mean it will not cast an integral type to another integral
>type? What about pointer to pointer?

There are seven distinct types of cast that reinterpret_cast can
perform listed in the DWP, and it takes about a page to describe them.
I won't reproduce all that here.

The short answer is that it is intended for non-portable casts whose
results can be expected to vary or be invalid on different platforms.
I would expect all reinterpret_casts in a program to be restricted
to sections of code that are platform-specific and highly localized.
You don't want them randomly occurring in your code precisely because
you can't depend on portable results.

Conversion among integer types requires no cast. All such conversions
may be implicit. You could also use a static_cast for that purpose.
Reinterpret_cast among integer types seems to be disallowed. (I can't
think of a good reason for wanting to use one, but maybe you can.)

Reinterpret_cast can be used to cast among many different kinds of
pointer types, as long as you don't cast away constness. You should
not use it for operations allowed as static_casts or dynamic_casts,
because the proper value adjustments might not occur.

---
Steve Clamage, stephen.clamage@eng.sun.com
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]