difference between static_cast and reinterpret_cast

P

Peter

I never used reinterpret_cast -- probably because I don't know what it
means.
Can somebody enlighten me?
I looked into Stroustrup's "The annoted C++ reference manual" -- but
this was no help.
Can I assume that reinterpret_cast is not safe and should not be used?
Does it always succeed even if the cast is garbage?
 
V

Victor Bazarov

Peter said:
I never used reinterpret_cast -- probably because I don't know what it
means.
Can somebody enlighten me?
I looked into Stroustrup's "The annoted C++ reference manual" -- but
this was no help.

Get a newer book. ARM is good, but Acc'd C++ or TC++PL are probably
more up to date on many things...

'reinterpret_cast' is used to convert pointers to objects to integral
values (and back), if there is a type that can hold the entire value;
between pointers of different functions; between pointers and references
of unrelated object types.
Can I assume that reinterpret_cast is not safe and should not be used?

No. It should be used when the requirements call for it.
Does it always succeed even if the cast is garbage?

No.

V
 
F

Frederick Gotham

Peter posted:
I never used reinterpret_cast -- probably because I don't know what it
means.
Can somebody enlighten me?
I looked into Stroustrup's "The annoted C++ reference manual" -- but
this was no help.
Can I assume that reinterpret_cast is not safe and should not be used?
Does it always succeed even if the cast is garbage?


I use reinterpret_cast where I can't use static_cast.
 
T

Thomas Tutone

Frederick said:
Peter posted:



I use reinterpret_cast where I can't use static_cast.

Sorry to hear that.

(1) Sometime when static_cast doesn't work, it's because you have a
const issue - you should use a const_cast (rather than a
reinterpret_cast) or figure out why you have a const problem.

(2) The strategy of "If this hammer (static_cast) doesn't work, I'll
just get myself a bigger hammer" is generally a bad one. Unless you're
doing the kind of low-level casts Victor B. described, reinterpret_cast
should not be necessary.

Best regards,

Tom
 
V

Victor Bazarov

Peter said:
so reinterpret_cast can fail. During runtime or during compile time?

It should only "fail" (be flagged as inappropriate) during compile time.
The run-time stuff is usually simply undefined behaviour. You cannot
detect it.

V
 
R

Ron Natalie

Thomas said:
(1) Sometime when static_cast doesn't work, it's because you have a
const issue - you should use a const_cast (rather than a
reinterpret_cast) or figure out why you have a const problem.

A reinterpret_cast won't bash const either. You have to use
(preferably) a const_cast OR a C-style cast.
]
 
B

bjarne

The ARM is from 1989. It has been outdated for quite some time. For a
standard, see the ISO standard (link to a draft and explanation of how
to get the final version on my C++ page). For explanations, see D&E or
TC++PL3 (or any other complete modern C++ book).

The "new style casts" postdate the ARM. "reinterpret_cast" is the least
constrained.new cast. You use it if/when you really need to break the
type system. In goodc code, that's rare.

-- Bjarne Stroustrup; http://www.research.att.com/~bs
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top