Should I use static_cast or reinterpret_cast when casting a void* to whatever

C++PointersStatic CastReinterpret Cast

C++ Problem Overview


Both static_cast and reinterpret_cast seem to work fine for casting void* to another pointer type. Is there a good reason to favor one over the other?

C++ Solutions


Solution 1 - C++

Use static_cast: it is the narrowest cast that exactly describes what conversion is made here.

There’s a misconception that using reinterpret_cast would be a better match because it means “completely ignore type safety and just cast from A to B”.

However, this doesn’t actually describe the effect of a reinterpret_cast. Rather, reinterpret_cast has a number of meanings, for all of which holds that “the mapping performed by reinterpret_cast is implementation-defined.” [5.2.10.3]

But in the particular case of casting from void* to T* the mapping is completely well-defined by the standard; namely, to assign a type to a typeless pointer without changing its address.

This is a reason to prefer static_cast.

Additionally, and arguably more important, is the fact that every use of reinterpret_cast is downright dangerous because it converts anything to anything else really (for pointers), while static_cast is much more restrictive, thus providing a better level of protection. This has already saved me from bugs where I accidentally tried to coerce one pointer type into another.

Solution 2 - C++

The static_cast is more appropriate for converting a void* to a pointer of some other type.

static_cast is the cast of choice when there is a natural, intuitive conversion between two types that isn't necessarily guaranteed to work at runtime. For example, you can use static_cast to convert base class pointers to derived class pointers, which is a conversion that makes sense in some cases but can't be verified until runtime. Similarly, you can use static_cast to convert from an int to a char, which is well-defined but may cause a loss of precision when executed.

reinterpret_cast, on the other hand, is a casting operator designed to do conversions that are fundamentally not safe or not portable. For example, you can use reinterpret_cast to convert from a void * to an int, which will work correctly if your system happens to have sizeof (void*)sizeof (int). You can also use reinterpret_cast to convert a float* to an int* or vice-versa, which is platform-specific because the particular representations of floats and ints aren't guaranteed to have anything in common with one another.

In short, if you ever find yourself doing a conversion in which the cast is logically meaningful but might not necessarily succeed at runtime, avoid reinterpret_cast. static_cast is a good choice if you have some advance knowledge that the cast is going to work at runtime, and communicates to the compiler "I know that this might not work, but at least it makes sense and I have a reason to believe it will correctly do the right thing at runtime." The compiler can then check that the cast is between related types, reporting a compile-time error if this isn't the case. Using reinterpret_cast to do this with pointer conversions completely bypasses the compile-time safety check.

There are a few circumstances where you might want to use a dynamic_cast instead of a static_cast, but these mostly involve casts in a class hierarchy and (only rarely) directly concern void*.

As for which one is preferred by the spec, neither is overly mentioned as "the right one to use" (or at least, I don't remember one of them being mentioned this way.) However, I think the spec wants you to use static_cast over reinterpret_cast. For example, when using a C-style cast, as in

A* ptr = (A*) myVoidPointer;

The order of casting operators that's tried always tries to use a static_cast before a reinterpret_cast, which is the behavior you want since reinterpret_cast isn't guaranteed to be portable.

Solution 3 - C++

This is a tough question. On the one hand, Konrad makes an excellent point about the spec definition for reinterpret_cast, although in practice it probably does the same thing. On the other hand, if you're casting between pointer types (as is fairly common when indexing in memory via a char*, for example), static_cast will generate a compiler error and you'll be forced to use reinterpret_cast anyway.

In practice I use reinterpret_cast because it's more descriptive of the intent of the cast operation. You could certainly make a case for a different operator to designate pointer reinterprets only (which guaranteed the same address returned), but there isn't one in the standard.

Solution 4 - C++

You likely obtained that void* with implicit conversion, so you should use static_cast because it is closest to the implicit conversion.

Solution 5 - C++

Use static_cast for this. Only in the rarest of rare cases when there is no other way use reinterpret_cast.

Solution 6 - C++

Casting to and from void* using static_cast and using reinterpret_cast is identical. See the answer at the link. But usually static_cast is preferred because it is more narrow and in general (but not in this specific case) more safe conversion.

Solution 7 - C++

I suggest using the weakest possible cast always.

reinterpret_cast may be used to cast a pointer to a float. The more structure-breaking the cast is, the more attention using it requires.

In case of char*, I'd use c-style cast, until we have some reinterpret_pointer_cast, because it's weaker and nothing else is sufficient.

Solution 8 - C++

reinterpret_cast will forcefully convert the void* to the target data type. It doesn't guarantee any safety and your program might crash as the underlying object could be anything.

For ex, you could typecast an myclass* to void* and then use reinterpret_cast to convert it to yourclass* which may have a completely different layout.

So its better and recommended to use static_cast

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndyView Question on Stackoverflow
Solution 1 - C++Konrad RudolphView Answer on Stackoverflow
Solution 2 - C++templatetypedefView Answer on Stackoverflow
Solution 3 - C++NickView Answer on Stackoverflow
Solution 4 - C++sharptoothView Answer on Stackoverflow
Solution 5 - C++AshaView Answer on Stackoverflow
Solution 6 - C++anton_rhView Answer on Stackoverflow
Solution 7 - C++Pavel RadzivilovskyView Answer on Stackoverflow
Solution 8 - C++mukeshkumarView Answer on Stackoverflow