宏映射整数字符串令牌(Macro for mapping integer to string for

2019-10-19 20:37发布

我需要一个宏TAG(N,C)其需要将参数NC生成表单TAG_ + f的令牌(N)+ G(C),其中, f(1) = Af(2) = ABf(3) = ABCf(4) = ABCDg(0) = (empty string)g(1) = _FOOg(2) = _BAR

例子:

TAG(3,0) -> TAG_ABC
TAG(2,1) -> TAG_AB_FOO
TAG(4,2) -> TAG_ABCD_BAR

做这个的最好方式是什么?

Answer 1:

您可以使用下列内容:

#define f_1 A
#define f_2 AB
#define f_3 ABC
#define f(N) f_##N

#define g_0 /* Empty */
#define g_1 _FOO
#define g_2 _BAR
#define g(N) g_##N

#define CONCAT3_(A, B, C) A ## B ## C
#define CONCAT3(A, B, C) CONCAT3_(A, B, C)
#define TAG(N, C) CONCAT3(TAG_, f(N), g(C))


文章来源: Macro for mapping integer to string for token
标签: c++ macros