C pre-processor macro expansion

こ雲淡風輕ζ 提交于 2019-12-18 23:46:09

问题


I'm trying to do (what I would have thought) was a simple macro expansion

#define CLEAR_DIGIT(a,b)    iconMap[a] &= ~(b)
#define R1 4, 16
CLEAR_DIGIT(R1);

Now I would expect that to expand to CLEAR_DIGIT(4,16) which expands to iconMap[4] &= ~16 However, it doesn't... If I make CLEAR_DIGIT a function:

void ClearDigit(unsigned char a, unsigned char b)
{
    iconMap[a] &= ~b;
}
#define R1 4, 16
ClearDigit(R1);

then it works fine, so R1 being expanded out to the two arguments isn't an issue... Is there any way of forcing it to expand R1 before doing the macro function expansion?


回答1:


You could use a helper macro. See also double-stringize problem

#define CLEAR_DIGIT_HELPER(a,b) iconMap[a] &= ~(b)
#define CLEAR_DIGIT(x) CLEAR_DIGIT_HELPER(x)
#define R1 4, 16
CLEAR_DIGIT(R1);


来源:https://stackoverflow.com/questions/8587965/c-pre-processor-macro-expansion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!