Possible to check return type of a function?

▼魔方 西西 提交于 2019-12-25 12:08:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!