Passing variable type as function parameter

后端 未结 4 2024
孤独总比滥情好
孤独总比滥情好 2020-12-24 12:03

Is it possible to pass variable type as part of a function parameter, e.g.:

void foo(varType type)
{
  // Cast to global static
  unsigned char bar;
  bar =          


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-24 12:27

    You can't do that for a function, because then it needs to know the types of the arguments (and any other symbols the function uses) to generate working machine code. You could try a macro like:

    #define foo(type_t) ({ \
        unsigned char bar; \
        bar = ((type_t*)(&static_array))->member; \
        ... \
        })
    

提交回复
热议问题