【发布时间】:2016-07-28 15:10:45
【问题描述】:
我正在研究一个递归宏。但是,它似乎没有递归扩展。这是一个最小的工作示例来说明我的意思:
// ignore input, do nothing
#define ignore(...)
// choose between 6 names, depending on arity
#define choose(_1,_2,_3,_4,_5,_6,NAME,...) NAME
// if more than one parameter is given to this macro, then execute f, otherwise ignore
#define ifMore(f,...) choose(__VA_ARGS__,f,f,f,f,f,ignore)(__VA_ARGS__)
// call recursively if there are more parameters
#define recursive(first,args...) first:ifMore(recursive,args)
recursive(a,b,c,d)
// should print: a:b:c:d
// prints: a:recursive(b,c,d)
recursive 宏应递归扩展自身并始终连接结果,用冒号分隔。但是,它不起作用。递归宏生成正确(从结果a:recursive(b,c,d) 可以看出,其中再次包含对宏的格式良好的调用),但生成的递归调用未扩展。
为什么会这样,我怎样才能得到我想要的行为?
【问题讨论】:
-
'我正在研究一个递归宏' - 好吧,很难知道该说什么:(非递归宏是一个 PITA。
标签: c macros c-preprocessor