动态变量名称:用文本连接宏定义

non*_*ipt 2 c c-preprocessor

是否可以使用常规文本加入宏定义?

例如;

#define T_NAME      Text_Int       
#define MAP_KEYS    T_NAME _Map_Keys      // defined in a separate header file "a.h"

// I am hoping MAP_KEYS = Text_Int_Map_Keys
Run Code Online (Sandbox Code Playgroud)

所以我可以去:

#undef T_NAME
#undef MAP_KEYS
#define T_NAME      Text_Real       
#define MAP_KEYS    T_NAME _Map_Keys

#undef T_NAME
#undef MAP_KEYS
#define T_NAME      Text_Short       
#define MAP_KEYS    T_NAME _Map_Keys
Run Code Online (Sandbox Code Playgroud)

oua*_*uah 5

使用CAT宏:

#define CAT(x, y) CAT_(x, y)
#define CAT_(x, y) x ## y

#define T_NAME      Text_Int 
#define MAP_KEYS    CAT(T_NAME, _MAP_KEYS)
Run Code Online (Sandbox Code Playgroud)

然后

MAP_KEYS
Run Code Online (Sandbox Code Playgroud)

将扩展到Text_Int_Map_Keys.

由于普通操作数##在粘贴之前没有扩展,因此需要间接。