Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 宏作为宏参数展开不工作_C_C Preprocessor - Fatal编程技术网

C 宏作为宏参数展开不工作

C 宏作为宏参数展开不工作,c,c-preprocessor,C,C Preprocessor,为什么下面的最小示例不起作用 static inline int test_1(int x) { return x; } #define TEST(a, ...) test_##a(__VA_ARGS__) #define ONE 1 void temp() { TEST(1, 5); // OK TEST(ONE, 5); // error: use of undeclared identifier 'test_ONE' } 根据我的理解,宏中的宏只要不是递归的就应该工作。如

为什么下面的最小示例不起作用

static inline int test_1(int x) { return x; }
#define TEST(a, ...) test_##a(__VA_ARGS__)
#define ONE 1
void temp() {
    TEST(1, 5); // OK
    TEST(ONE, 5); // error: use of undeclared identifier 'test_ONE'
}

根据我的理解,宏中的宏只要不是递归的就应该工作。

如果使用
-E
选项,可以在预处理器阶段后看到

宏在多个过程中展开,并具有自己的优先顺序

在这种情况下,
TEST(ONE,5)
直接扩展到
TEST\u ONE(5)

如果添加单个间接层(),则可以在看到
##
操作符之前使
ONE
展开

#define TEST_I(a,...)test_##a(__VA_ARGS__)
#define TEST(a, ...) TEST_I(a,__VA_ARGS__)
在第一次通过时,
测试(一,5)
被扩展为
测试I(一,5)
,然后
测试I(1,5)
,在第二次通过时,它成为
测试I(5)


TEST\u I
在第一次通过时没有扩展,因为它是通过扩展
TEST
生成的,而
ONE
已经存在。

您缺少了一个额外的间接层次:
#定义CONCAT(a,b)a#b\n#定义TEST(a,…)CONCAT(TEST,a)(u VA_uargs)
,但我不知道为什么有必要。
test##a
使
test#ONE
得到评估,而不是
ONE
(参见此处:)