如何用宏删除括号?

2022-01-11 00:00:00 macros c++ c-preprocessor

宏参数中不允许使用逗号,因为它将被视为多个参数,并且预处理将是错误的.但是,我们可以将参数括起来,让预处理器将其视为一个参数.是否有可以删除括号的宏或其他技术?

No comma is allowed in a macro argument because it will be treated as more than one arguments and the preprocessing will be wrong. However, we can parenthesize the argument to let preprocessor treat it as one argument. Is there a macro or other techniques which can remove the enclosing parentheses?

例如,如果我定义一个宏

For example, if I define a macro like

#define MY_MACRO(a, b)   ...

并像使用它

MY_MACRO( A<int, double>, text );

会错的.像这样使用它

MY_MACRO( (A<int, double>), text)

使用宏或技术来删除括号就可以了.Boost 提供了 BOOST_IDENTITY_TYPE 宏,只适用于类型而不是一般情况

with a macro or technique to remove the parentheses will be fine. Boost provides BOOST_IDENTITY_TYPE macro for only types but not general cases

推荐答案

#define ESC(...) __VA_ARGS__

然后

MY_MACRO( ESC(A<int, double>), text );

可以为所欲为.

相关文章