How to remove the enclosing parentheses with macro?(如何用宏删除括号?)
问题描述
宏参数中不允许使用逗号,因为它将被视为多个参数,并且预处理将是错误的.但是,我们可以将参数括起来,让预处理器将其视为一个参数.是否有可以删除括号的宏或其他技术?
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 );
可以为所欲为.
这篇关于如何用宏删除括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何用宏删除括号?
基础教程推荐
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- CString 到 char* 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 初始化列表*参数*评估顺序 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
