问题
Let's say I have a function..
void * getValue(...);
Is there a way that I check for the return type of a call to getValue(...) ?
I plan on using a void* method such as getValue() in a program and the process of my program will be dependent on the return type of getValue().
Is it possible to check for return type?
回答1:
You may want to return a structure instead
enum valuetype {PCHAR, PSHORT, PINT, PLONG, PLLONG, PFLOAT, PDOUBLE, PLDOUBLE};
struct sometype {
enum valuetype vt;
void *value;
}
struct sometype getValue(...);
回答2:
I'm afraid there's no way you can tell exactly what's stored in a (void *) pointer. If you want my advise, change your code and use proper typed pointers
回答3:
No - C does not tag values with their type, so the information is not available.
Possible alternatives include adding an out parameter to the function to indicate the type, or making/using a variant struct that includes both the value and an enum indicating its type.
来源:https://stackoverflow.com/questions/23118117/possible-to-check-return-type-of-a-function