Reputation: 54551
So I was reading the answers to dynamic_cast from "void *" and although you can't cast from a void *
to a T *
several of the responses point out that it is possible to cast a T *
to a void *
, but don't give any indication why you'd want to do that.
Is this just a bit of trivia that it's possible, or is there a case where it would make sense? I thought about maybe for readability or to make it clear that we're converting to a void *
, but given the purpose of dynamic_cast
, it doesn't fit very well to me.
For that matter, is there any reason to do anything other than let T *
become void *
implicitly? I've seen C-style casts to void *
used from time to time for this purpose, I presume just to be explicit (assuming we're not doing something unusual like casting int
to a pointer or something).
Upvotes: 14
Views: 3402
Reputation: 336
It can be useful when implementing wrappers for operators new and delete. The thing is that operator delete allows us to deallocate memory using pointer to a base of a polymorphic object. In such situation, the wrapper would not know, which object is really being deallocated there (out of all the objects that were allocated by the wrapper before). So we use dynamic_cast< void * >() as unique identity of the object - it casts to the most derived type of a polymorphic object. This allow us to keep track of all the objects currently allocated through this wrapper. Such wrapper can be used for memory allocation tracking and for batch deallocations.
Upvotes: 1
Reputation: 299750
First, when using dynamic_cast<void*>(x)
you get a pointer to the first byte of the most derived object. As long as the static type of x
is polymorphic.
This can be useful in a handful of scenarios, where the address serves as object identity:
Granted, this is certainly not a daily usage, but in C++ the memory address is a de-facto identifier for objects, so a mechanism to access it from any part of the inheritance hierarchy certainly is useful for those few edge cases.
Upvotes: 11
Reputation: 473262
There is a purpose to this, kinda. It is hinted at in the part of the spec that allows it. From N3337, section 5.2.7, paragraph 7:
If T is “pointer to cv void,” then the result is a pointer to the most derived object pointed to by v.
So a dynamic_cast<void*>(...)
is really shorthand for static_cast<void*>(dynamic_cast<MostDerivedType*>(...))
. And that would be useful... sort of.
The difficulty in making it useful is that you don't necessarily know what MostDerivedType
is. After all, it can be different for each expression. So once you have it as a void*
, you don't necessarily have a way to cast it back safely. If you make a guess about MostDerivedType
and just static_cast
it, and you're wrong, then you're in undefined behavior land. Whereas if you do the dynamic_cast
to that type (then static_cast
to void*
), it will at least return NULL if it isn't of that type.
So no, I would say that it isn't very useful. Not if you want to live within the boundaries of C++ and not rely on potentially undefined behavior.
Upvotes: 6