468,491 Members | 2,114 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,491 developers. It's quick & easy.

difference between static_cast and reinterpret_cast

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?

Aug 8 '06 #1
7 10333
Peter wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 8 '06 #2

Victor Bazarov wrote:
Does it always succeed even if the cast is garbage?

No.
so reinterpret_cast can fail. During runtime or during compile time?

Aug 8 '06 #3
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.

--

Frederick Gotham
Aug 8 '06 #4
Frederick Gotham wrote:
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.
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

Aug 8 '06 #5
Peter wrote:
Victor Bazarov wrote:
>>Does it always succeed even if the cast is garbage?

No.

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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 8 '06 #6
Thomas Tutone wrote:
>
(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.
]
Aug 9 '06 #7

Thomas Tutone wrote:
Frederick Gotham wrote:
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?
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

Aug 9 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by buds | last post: by
11 posts views Thread by Scott Brady Drummonds | last post: by
26 posts views Thread by Steven T. Hatton | last post: by
24 posts views Thread by Rahul | last post: by
2 posts views Thread by Lucy Ludmiller | last post: by
1 post views Thread by =?utf-8?B?5Zyw55CD5Y+R5Yqo5py6?= | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.