我想在與其他連接之前評估一個令牌。 「問題」是該標準規定了行爲在##連接之前評估預處理器令牌
進行復核之前更換名單更宏觀的名稱來代替 ,一個##預處理標記的每個實例替換 列表(而不是從一個參數)被刪除,並且前面的預處理令牌與以下預處理令牌連接在一起。
在下面的例子中
因此,
#include <stdlib.h>
struct xy {
int x;
int y;
};
struct something {
char * s;
void *ptr;
int size;
struct xy *xys;
};
#define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
#define DECLARE_XY_BEGIN(prefix) \
struct xy prefix ## _xy_table[] = {
#define XY(x, y) {x, y},
#define DECLARE_XY_END(prefix) \
{0, 0} \
}; \
struct something prefix ## _something = { \
"", NULL, \
ARRAY_SIZE(prefix ## _xy_table), \
&(prefix ## _xy_table)[0], \
};
DECLARE_XY_BEGIN(linear1)
XY(0, 0)
XY(1, 1)
XY(2, 2)
XY(3, 3)
DECLARE_XY_END(linear1)
#define DECLARE_XY_BEGIN_V2() \
struct xy MYPREFIX ## _xy_table[] = {
#define DECLARE_XY_END_V2() \
{0, 0} \
}; \
struct something MYPREFIX ## _something = { \
"", NULL, \
ARRAY_SIZE(MYPREFIX ## _xy_table), \
&(MYPREFIX ## _xy_table)[0], \
};
#define MYPREFIX linear2
DECLARE_XY_BEGIN_V2()
XY(0, 0)
XY(2, 1)
XY(4, 2)
XY(6, 3)
DECLARE_XY_END_V2()
#undef MYPREFIX
最後聲明擴展到
struct xy MYPREFIX_xy_table[] = {
{0, 0},
{2, 1},
{4, 2},
{6, 3},
{0, 0} }; struct something MYPREFIX_something = { "", 0, (sizeof(MYPREFIX_xy_table)/sizeof((MYPREFIX_xy_table)[0])), &(MYPREFIX_xy_table)[0], };
,而不是
struct xy linear2_xy_table[] = {
{0, 0},
{2, 1},
{4, 2},
{6, 3},
{0, 0} }; struct something linear2_something = { "", 0, (sizeof(linear2_xy_table)/sizeof((linear2_xy_table)[0])), &(linear2_xy_table)[0], };
像我想。有沒有某種方法來定義生成這個宏的宏?第一組宏是,但我想避免前綴重複,只有這一次定義。那麼是否可以使用#define
來設置前綴並讓宏使用它?
可能重複[如何使用C預處理器的兩倍串聯和擴大宏在「ARG ## \ _# #MACRO「?](http://stackoverflow.com/questions/1489932/how-to-concatenate-twice-with-the-c-preprocessor-and-expand-a-macro-as-in-arg)請嘗試以最小化示例:-) –