2011-10-17 98 views
9

我想在与其他连接之前评估一个令牌。 “问题”是该标准规定了行为在##连接之前评估预处理器令牌

进行复核之前更换名单更宏观的名称来代替 ,一个##预处理标记的每个实例替换 列表(而不是从一个参数)被删除,并且前面的预处理令牌与以下预处理令牌连接在一起。

在下面的例子中

因此,

#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来设置前缀并让宏使用它?

+0

可能重复[如何使用C预处理器的两倍串联和扩大宏在“ARG ## \ _# #MACRO“?](http://stackoverflow.com/questions/1489932/how-to-concatenate-twice-with-the-c-preprocessor-and-expand-a-macro-as-in-arg)请尝试以最小化示例:-) –

回答

4

您可以使用第二级扩展,例如。

#define XY_HLP1(a) DECLARE_XY_BEGIN(a) 
#define XY_HLP2(a) DECLARE_XY_END(a) 
#define DECLARE_XY_BEGIN_V2() XY_HLP1(MYPREFIX) 
#define DECLARE_XY_END_V2() XY_HLP2(MYPREFIX) 
+0

是的,我喜欢这个。不需要更改旧的宏并为其他人创建可能的合并问题,并且即使双扩展可能不能直观地理解,仍然显而易见的是,两个地方都使用相同的前缀。 – hlovdal

11

You can use a macro for concatenation

#define CONCAT_(A, B) A ## B 
#define CONCAT(A, B) CONCAT_(A, B) 

这工作那么

#define A One 
#define B Two 
CONCAT(A, B) // Results in: OneTwo 
+1

我从来不明白为什么这样的事情需要两个层次。是宏扩展的第一级,而实际级联的第二级? – ijustlovemath