I am little confused with the applicability of reinterpret_cast
vs static_cast
. From what I have read the general rules are to use static cast when the types can be interpreted at compile time hence the word static
. This is the cast the C++ compiler uses internally for implicit casts also.
reinterpret_cast
s are applicable in two scenarios, convert integer types to pointer types and vice versa or to convert one pointer type to another. The general idea I get is this is unportable and should be avoided.
Where I am a little confused is one usage which I need, I am calling C++ from C and the C code needs to hold on to the C++ object so basically it holds a void*
. What cast should be used to convert between the void *
and the Class type?
I have seen usage of both static_cast
and reinterpret_cast
? Though from what I have been reading it appears static
is better as the cast can happen at compile time? Though it says to use reinterpret_cast
to convert from one pointer type to another?
The C++ standard guarantees the following:
static_cast
ing a pointer to and fromvoid*
preserves the address. That is, in the following, a, b and c all point to the same address:reinterpret_cast
only guarantees that if you cast a pointer to a different type, and thenreinterpret_cast
it back to the original type, you get the original value. So in the following:a and c contain the same value, but the value of b is unspecified. (in practice it will typically contain the same address as a and c, but that's not specified in the standard, and it may not be true on machines with more complex memory systems.)
For casting to and from void*,
static_cast
should be preferred.One case when
reinterpret_cast
is necessary is when interfacing with opaque data types. This occurs frequently in vendor APIs over which the programmer has no control. Here's a contrived example where a vendor provides an API for storing and retrieving arbitrary global data:To use this API, the programmer must cast their data to
VendorGlobalUserData
and back again.static_cast
won't work, one must usereinterpret_cast
:Below is a contrived implementation of the sample API:
I tried to conclude and wrote a simple safe cast using templates. Note that this solution doesn't guarantee to cast pointers on a functions.
Read the FAQ! Holding C++ data in C can be risky.
In C++, a pointer to an object can be converted to
void *
without any casts. But it's not true the other way round. You'd need astatic_cast
to get the original pointer back.