How to evaluate a nested preprocessor macro

喜夏-厌秋 提交于 2020-01-11 05:18:17

问题


let's say I want to select the behaviour of a certain preprocessor directive evaluating at compile time the concatenation of a constant string and the result of another macro.

#define CASE1 text1
#define CASE2 text2
#define CASE3 text3
#define SCENARIO 3
/** the following won't work - for examplification purposes only**/
#define FUNCTION CASE##SCENARIO

/** whenever I write FUNCTION, I expect to see text3 **/

I am having an hard time thinking of a viable solution, as the preprocessor is a one-pass beast. Is that even feasible ?


回答1:


It's possible, you just need to add some extra layers of macros. The key is that when you use the token-pasting operator ##, the preprocessor will not expand its operands. But, if you add another layer of macros, the preprocessor will expand those arguments. For example:

#define CASE1 text1
#define CASE2 text2
#define CASE3 text3
#define SCENARIO 3

#define TOKENPASTE_HELPER(x, y) x ## y
#define TOKENPASTE(x, y) TOKENPASTE_HELPER(x, y)
#define FUNCTION TOKENPASTE(CASE, SCENARIO)

When the preprocessor expands FUNCTION, it expands TOKENPASTE. When it expands TOKENPASTE, it expands its arugments (so SCENARIO gets replaced by 3), since neither of its arguments are operands of the token-pasting operator. Next, it expands TOKENPASTE_HELPER, which does the actual token pasting to make CASE3. Finally, it expands the CASE3 macro to get text3.



来源:https://stackoverflow.com/questions/13074432/how-to-evaluate-a-nested-preprocessor-macro

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