C generic Parameter into Function pointer -


is possible in c(not c++) have fuction pointer takes generic value(not pointer), -pedantic , -wall -werror flags set.

note: can't change parameter type. code has support uint8_t, uint16_t, etc... types parameters

goal: solve problem code.

question

is there way typecast uint8_t(and/or uint16_t) parameter void*(approach1)? pass non-pointer type value void* value.

is there way setup generic type work different values(approach 2)?

last resort there way set specific compiler exception in code?(this question has been answer)

approach 1(causes invalid conversion uint8_t void*)

typedef struct {     void (*set_func)(void*); } setfunction;  void setvalue(uint8_t bytevalue)//not pointer parameter {    bytevalue++; }  void setshortvalue(uint16_t bytevalue)//not pointer parameter {    bytevalue++; }  int main() {     uint8_t = 123;     uint16_t b = 321;     setfunction pointerfuncion;     setfunction pointerfuncionshort;      //typecast setvalue appease compiler warning     pointerfunction.set_func = (void(*)(void*))&setvalue;     pointerfuncionshort.set_func = (void(*)(void*))&setshortvalue;       //use function pointer non-pointer parameter     // compile error thrown invalid conversion uint8_t void*     pointerfunction.set_func(a);     pointerfuncionshort.set_func(b); } 

aprroach 2(causes many parameters compile error)

 typedef struct {     void (*set_func)();//blank parameter allow multiple args } setfunction;  void setvalue(uint8_t bytevalue)//not pointer parameter {    bytevalue++; }  void setshortvalue(uint16_t bytevalue)//not pointer parameter {    bytevalue++; }  int main() {     uint8_t = 123;     uint16_t b = 321;      setfunction pointerfuncion;     setfunction pointerfuncionshort;      //typecast setvalue appease compiler warning     pointerfunction.set_func = (void(*)())&setvalue;     pointerfuncionshort.set_func = (void(*)())&setshortvalue;      //use function pointer non-pointer parameter     pointerfunction.set_func(a);// compile error thrown "too many args"     pointerfuncionshort.set_func(b);// compile error thrown "too many args" } 

update

to add clarity problem. have 100's of functions 1 parameter. 1 parameter of functions different types. can't change of functions, want have 1 function pointer type(or more based on type) of functions. can change of types associated function pointer , type function pointer, not pointing too.

no, not.

simple answer: called function not know how fetch argument.

details: function code fixed when called (executed). contains code access argument, depends on type of arguemtn (e.g. uint32_t, 32 bit load/soter required, uint8_t 8 bit load/store). cannot handle value fetch properly. different c++ , higher languages python, c not have concept of run-time type identification built-in.

however, can pass union function , handle each variant in function seperately. generate possible accesses. however, have specify actual type being passed. done second argument specifies actual type. union struct composed of type-identifier , actual value. envelope, still explicit.

typedef union {     int i;     float f; } myvalue;  typedef enum {     my_value_int,     my_value_float } myvaluetype;  void func(myvaluetype type, myvalue value) {     switch ( type ) {         ...     } }  int main(void) {     func(my_value_int, (myvaluetype){ .i=1 }); } 

the compound literal argument works constants, otherwise have assign value union first (or use union). gcc has extension avoid this, can have function takes such union, caller may use simple cast, instead of compound literal. works variables, too:

    func(my_value_float, (myvaluetype)1.0); 

an alternative passing const void * , internally casting. however, more risky union approach.

all approaches require pasing actual type explicitly (e.g. using enum).

c11 allows create macro evaluates different expressions, according type of argument using new _generic construct. original approach can simulated (using gcc extension, normal way possible, more complicated):

// use code of first block here  #define func_generic(val) _generic((val), \     int : func(my_value_int, (myvaluetype)(val)), \     int : func(my_value_int, (myvaluetype)(val)) )  // , call like: func_generic(1); func_generic(1.0); 

however, note restriction of _generic: no 2 compatible types allowed type-selector (i.e. const int , int not allowed both) uint16_t , uint32_t works, however.

note gcc (you apparently use) supports c11 using -std=c11 or std=gnu11. latter enables gnu-extensions.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -