如何在 C++ 宏参数中使用括号?
How can I use parentheses in C++ macro parameters?
我需要在宏的实际参数中使用圆括号,但圆括号似乎改变了分隔宏参数的逗号的行为。
我让我的预处理器将其输出转储到一个文本文件中,这样我就可以看到它正在生成什么。
然后我进行了基本测试以确认行为。
#define MACRO_TEST_1( X , Y ) X && Y
MACRO_TEST_1 ( A , B )
// Desired result: A && B
// Actual result: A && B
MACRO_TEST_1 ( ( C , D ) )
// Desired result: ( C && D )
// Actual result: ( C , D ) &&
// Warning: "not enough actual parameters for macro 'MACRO_TEST_1'"
似乎向第一个参数添加一个左括号,向第二个参数添加一个右括号,导致预处理器将逗号视为 part 第一个参数,因此假设我 根本没有提供 第二个参数。
警告以及预处理器输出在 &&
.
之后不显示任何内容都证明了这一点
所以我的问题是,即使参数中有括号,我如何告诉预处理器逗号分隔参数?
我尝试转义括号或逗号,但这没有任何区别。
(结果相同,只是在预处理器输出中插入了转义字符。)
So my question is, how can I tell the preprocessor that the comma
separates the parameters, even though the parameters have parentheses
in them?
我认为没有办法,至少在这样的宏实现中是这样。
来自gcc.gnu.org:
To invoke a macro that takes arguments, you write the name of the
macro followed by a list of actual arguments in parentheses, separated
by commas.
Leading and trailing whitespace in each argument is dropped, and all
whitespace between the tokens of an argument is reduced to a single
space. Parentheses within each argument must balance; a comma within
such parentheses does not end the argument.
You cannot leave out arguments entirely; if a macro takes two
arguments, there must be exactly one comma at the top level of its
argument list.
我在技术上找到了一个解决方案,虽然很丑陋。
您必须为括号字符定义符号。
#define OP (
#define CP )
#define MACRO_TEST_1( X , Y ) X && Y
MACRO_TEST_1 ( A , B )
// Desired result: A && B
// Actual result: A && B
MACRO_TEST_1 ( OP C , D CP )
// Desired result: ( C && D )
// Actual result: ( C && D )
我需要在宏的实际参数中使用圆括号,但圆括号似乎改变了分隔宏参数的逗号的行为。
我让我的预处理器将其输出转储到一个文本文件中,这样我就可以看到它正在生成什么。
然后我进行了基本测试以确认行为。
#define MACRO_TEST_1( X , Y ) X && Y
MACRO_TEST_1 ( A , B )
// Desired result: A && B
// Actual result: A && B
MACRO_TEST_1 ( ( C , D ) )
// Desired result: ( C && D )
// Actual result: ( C , D ) &&
// Warning: "not enough actual parameters for macro 'MACRO_TEST_1'"
似乎向第一个参数添加一个左括号,向第二个参数添加一个右括号,导致预处理器将逗号视为 part 第一个参数,因此假设我 根本没有提供 第二个参数。
警告以及预处理器输出在 &&
.
所以我的问题是,即使参数中有括号,我如何告诉预处理器逗号分隔参数?
我尝试转义括号或逗号,但这没有任何区别。
(结果相同,只是在预处理器输出中插入了转义字符。)
So my question is, how can I tell the preprocessor that the comma separates the parameters, even though the parameters have parentheses in them?
我认为没有办法,至少在这样的宏实现中是这样。
来自gcc.gnu.org:
To invoke a macro that takes arguments, you write the name of the macro followed by a list of actual arguments in parentheses, separated by commas.
Leading and trailing whitespace in each argument is dropped, and all whitespace between the tokens of an argument is reduced to a single space. Parentheses within each argument must balance; a comma within such parentheses does not end the argument.
You cannot leave out arguments entirely; if a macro takes two arguments, there must be exactly one comma at the top level of its argument list.
我在技术上找到了一个解决方案,虽然很丑陋。
您必须为括号字符定义符号。
#define OP (
#define CP )
#define MACRO_TEST_1( X , Y ) X && Y
MACRO_TEST_1 ( A , B )
// Desired result: A && B
// Actual result: A && B
MACRO_TEST_1 ( OP C , D CP )
// Desired result: ( C && D )
// Actual result: ( C && D )