Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

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?

share|improve this question
3  
A better question would be "How do I avoid using void pointers in my code?". This is not difficult to do - I can't remember the last time I used one. – anon Dec 14 '09 at 9:34
48  
@anon Apparently you've never worked with POSIX threads before then. – user470379 Dec 23 '10 at 20:00
2  
@user470379 Wow...that's the very reason I landed on this question at SO! Excellent observation :-). – Ogre Psalm33 Jun 21 '11 at 14:13

4 Answers 4

You should use a reinterpret_cast, because that describes better what you're doing (completely ignoring type safety)

This does actually not 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]

However, this is not what happens in this particular case, where the mapping is well-defined by the standard, namely assigning 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.

share|improve this answer
1  
In theory you're right on your first point, reinterpret_cast is not well-defined. In practice any implementation will do the same in both cases. IMO, the most self-documenting way should be preferred in that case and IMO that is reinterpret_cast. – Leon Timmermans Nov 21 '08 at 23:24
5  
@Leon: why is it more self-documenting? – jalf Apr 10 '10 at 18:09
3  
I guess that's personal, though I must admit I've been converted since posting that to static_cast too :-) – Leon Timmermans Apr 11 '10 at 11:04
1  
@jalf "why is it more self-documenting?" reinterpret_cast say what you want to do: reinterpret the same bytes using a different type, so clearly that's what you should use. – curiousguy Dec 20 '11 at 0:16
2  
@curiousguy Ah you were that guy (see other comment threat). Had I noticed, I wouldn’t even have risen to the bait. Have fun. – Konrad Rudolph Dec 21 '11 at 19:19

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.

share|improve this answer
1  
"different operator to designate pointer reinterprets only (which guaranteed the same address returned)" Hug? That operator is reinterpret_cast! – curiousguy Dec 19 '11 at 6:18

My personal preference is based on code literacy like this:

void* data = something;
MyClass* foo = reinterpret_cast<MyClass*>(data);
foo->bar();

or

typedef void* hMyClass; //typedef as a handle or reference
hMyClass = something;
const MyClass& foo = static_cast<MyClass&>(*hMyClass);
foo.bar();

They both do the same in the end, but static_cast seems more appropriate in a middle-ware, app enviroment, while reinterpret cast seems more like something you'd see in a lower-level library IMHO.

share|improve this answer
2  
The second code example won't compile. – Fred Nurk Jan 31 '11 at 9:50

I suggest using the weakest possible cast always.

reinterpret_cast is may be used to cast a pointer to a float. The more structure-breaking 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.

share|improve this answer
    
"reinterpret_cast is may be used to cast a pointer to a float." Certainly not! – curiousguy Dec 19 '11 at 6:16
    
certainly yes, curiousguy. Check the standard again. – Pavel Radzivilovsky Dec 19 '11 at 18:10
    
Can you give an example? – curiousguy Dec 19 '11 at 23:00
    
"Check the standard again." Have you checked the standard? – curiousguy Dec 21 '11 at 5:49
1  
Probably float f = *reinterpret_cast<const float*>(&p); – Ben Voigt Aug 7 '13 at 15:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.