How to concatenate twice with the C preprocessor and expand a macro as in “arg ## _ ## MACRO”?

前端 未结 3 1654
轮回少年
轮回少年 2020-11-21 10:03

I am trying to write a program where the names of some functions are dependent on the value of a certain macro variable with a macro like this:

#define VARIA         


        
3条回答
  •  温柔的废话
    2020-11-21 10:42

    #define VARIABLE 3
    #define NAME2(fun,suffix) fun ## _ ## suffix
    #define NAME1(fun,suffix) NAME2(fun,suffix)
    #define NAME(fun) NAME1(fun,VARIABLE)
    
    int NAME(some_function)(int a);
    

    Honestly, you don't want to know why this works. If you know why it works, you'll become that guy at work who knows this sort of thing, and everyone will come ask you questions. =)

    Edit: if you actually want to know why it works, I'll happily post an explanation, assuming no one beats me to it.

提交回复
热议问题