过载 C/C++关于其参数结构的预处理器宏
我想编写一个预处理器宏,如果它的参数是带括号的标记元组,则执行一件事,如下所示:
MY_MACRO((x, y))
如果它只是单个标记,则执行其他操作,如下所示:
MY_MACRO(x)
这可能吗?
如何区分空格分隔的标记数量,即 MY_MACRO(x)
和 MY_MACRO(xy)
?
请注意,我并没有尝试根据参数的数量进行重载 - 在所有情况下它都是一元宏。
编辑:如果有帮助,我愿意使用可变参数宏
I would like to write a preprocessor macro that does one thing if it's argument is a parenthesized tuple of tokens, like this:
MY_MACRO((x, y))
and something else if it's just a single token, like this:
MY_MACRO(x)
Is that possible?
How about distinguishing between the number of space-separated tokens, i.e. between MY_MACRO(x)
and MY_MACRO(x y)
?
Note that I am not trying to overload based on the number of arguments - it's a unary macro in all cases.
EDIT: I am willing to use variadic macros if they help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
至于你的第一个问题,以下宏可能会满足你的目的:
如果参数是单个标记,则宏
IS_SINGLE
将扩展为 1,否则,0。
希望这有帮助
As for your first question, the following macros might meet your purpose:
Macro
IS_SINGLE
is expanded to 1 if the argument is single token,otherwise, 0.
Hope this helps
使用 boost.preprocessor
它并不完全相同,因为即使单个参数情况也需要括号。但它确实允许可变数量的括号参数。
还有一种可能性:使用 BOOST_PP_IF、BOOST_PP_EQUAL 和 BOOST_PP_TUPLE_ELEM 执行类似以下操作
。
Using boost.preprocessor
It's not exactly the same as even a single argument case requires parenthesis. But It does allow a variable number of parenthesized arguments.
Also a possibility: Use the BOOST_PP_IF, BOOST_PP_EQUAL, and BOOST_PP_TUPLE_ELEM to do something like:
or so.