在无法识别的编译器上吞下可变参数宏中的逗号 ## [英] Swallowing comma in variadic macros on compilers that do not recognise ##
问题描述
我需要在 C 中编写一个可变参数宏,它必须接受零个或多个参数.
I need to write a variadic macro in C which must take zero or more arguments.
在 gcc 中,可以通过在逗号后添加##"来实现,例如,##____VA_ARGS____
在具有零参数的可变参数宏.
In gcc, that can be achieved by adding "##" after the comma, e.g. ,##____VA_ARGS____
as answered in Variadic macros with zero arguments.
但是,我的构建系统中的编译器(超出我的控制)不理解 ,##
语法,因此不会吞下逗号.
However, the compiler in my build system (beyond my control) does not understand the ,##
syntax and so does not swallow the comma.
有什么我可以使用的解决方法吗?
Is there a workaround I can use?
谢谢!
推荐答案
是的,gcc 吞下逗号是非标准的,你不应该依赖它.
Yes, gcc swallowing the comma is non standard and you should not rely on that.
使用符合 C99 的预处理器,您可以通过测试作为空标记的宏参数来实现类似的效果.有关其工作原理的想法,您可以查看这里,对于简化此类功能的编程的一整套预处理器宏,有 P99.
With C99 conforming preprocessors you may achieve a similar effect by testing for a macro arguments that is the empty token. For the ideas of how this works you could see here, for a whole set of preprocessor macros that ease the programming of such features there is P99.
这篇关于在无法识别的编译器上吞下可变参数宏中的逗号 ##的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!