Using commas inside a macro without parenthesis: How can I mix and match with a template?(在没有括号的宏中使用逗号:如何与模板混合和匹配?)
问题描述
考虑一个简单的宏:
#define ECHO(x) x
ECHO(foo(1, 2))
这会产生我们期望的准确输出:
This produces the exact output we expect:
foo(1, 2)
上面的例子有效,因为与函数调用相邻的括号被预处理器识别.
The above example works because the parenthesis adjacent to the function call are recognized by the preprocessor.
现在考虑如果我使用模板而不是函数调用会发生什么:
Now consider what happens if I use a template instead of a function call:
ECHO(template<int, bool>)
这会导致错误,因为预处理器将 template<int 和 bool> 解释为宏的两个单独参数.预处理器无法识别 <> 的作用域!
This causes an error because the preprocessor interprets the template<int and the bool> as two separate arguments to the macro. The preprocessor doesn't recognize <> for scope!
有没有在宏中使用这样的模板?
Is there anyway to use a template like this in a macro?
推荐答案
#define COMMA ,
ECHO(template<int COMMA bool>)
有点痛,但很管用.
FWIW,如果参数的语法允许 ()s,则不需要替换,例如,
FWIW, if the syntax for the argument allows ()s, you don't need the substitution, e.g.,
ECHO((a, b))
适用于单个参数宏,但不适用于所有情况(包括您的情况).
will work for a single argument macro but that doesn't work in all cases (including yours).
这篇关于在没有括号的宏中使用逗号:如何与模板混合和匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在没有括号的宏中使用逗号:如何与模板混合和匹配?
基础教程推荐
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- CString 到 char* 2021-01-01
