Getting type of void* data

I am wondering if it is possible to identify the type of data referenced by a void pointer if you do not know ahead of time what the possibilities are.

I would use this for debugging, when I receive a void* to something I cannot handle, I would like to print out a debug line with info on the unhandled type.

The complete context for my question is:

A library I am using has these functions on an object:
void* getUserPointer();
void setUserPointer(void* data);

This lets a developer set arbitrary data on the objects and then retrieve it later.
If I know I will only see my own objects, I can look for the types I know about:

1
2
3
4
5
6
void* data = obj->getUserPointer();
if (data && typeid(data) == typeid(TypeWeCanHandle))
{
     TypeWeCanHandle* myData = static_cast<TypeWeCanHandle*>(data);
     // do something with myData
}

But if my code traverses such an if-else block and still does not know what it is, I would like to to be able to print out a debug line with info about what the unhanded type is.

Is this possible?
closed account (zb0S216C)
hikethru08 wrote:
"I am wondering if it is possible to identify the type of data referenced by a void pointer if you do not know ahead of time what the possibilities are."

The only possible way you'll find out is by dereferencing the pointer, but dereferencing a void pointer isn't allowed since it could be pointing to any form of data. The compiler needs to know what type of data the pointer is pointing to, and that falls down to you with an explicit type-cast.

Wazzak

Last edited on
You could make a template function, or overloaded functions.
Topic archived. No new replies allowed.