递归C宏未扩展

杀人剂

我正在处理一个递归宏。但是,它似乎没有递归扩展。这是一个最小的工作示例来说明我的意思:

// 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)再次包含对宏的格式正确的调用),但是生成的递归调用并未扩展。

为什么会这样,如何获得所需的行为?

Nils_M

您无法获得想要的行为。根据设计,C预处理器尚未完成。

您可以使用多个宏来获取多个替换项,但是使用任意数量的替换项将无法实现真正​​的递归。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章