我正在尝试使用定义宏来实现我的记录器的逻辑系统,当定义某些切换时,它将扩展为空。问题是,当我堆叠多个这些开关时,一个嵌套在另一个内部(例如调用 IF_SWITCH_1 类似函数的宏),我得到了代码块中列出的多个错误。 是什么导致了这些错误?我该如何修复它们?
//Creation of the switches
#define _ADD_PARTS2(part1, part2, ...) part1 ## part2 (__VA_ARGS__)
#define _ADD_PARTS(part1, part2, ...) _ADD_PARTS2(part1, part2, __VA_ARGS__)
#define _LOGIC_SWITCH_(name, cond, ...) _ADD_PARTS(name, cond, __VA_ARGS__)
//Toggles
#define CONDITION_1 true
#define CONDITION_2 true
//Switches
#define IF_SWITCH_2_true(a, b, c) std::cout << "Passed" << std::endl
#define IF_SWITCH_2_false(...)
#define IF_SWITCH_2(a, b, c) _LOGIC_SWITCH_(IF_SWITCH_2_, CONDITION_1, a, b, c)
#define IF_SWITCH_1_true(a, b, c) IF_SWITCH_2(a, b, c)
#define IF_SWITCH_1_false(...)
#define IF_SWITCH_1(a, b, c) _LOGIC_SWITCH_(IF_SWITCH_1_, CONDITION_1, a, b, c)
//Use
IF_SWITCH_2(1, 1, 1); //Compiles and passes
IF_SWITCH_1(1, 1, 1); //"IF_SWITCH_2" was not declared in this scope;
//_LOGIC_SWITCH_ was not declared in this scope;
//Use of undeclared indentifier IF_SWITCH_2_
//Switching on and off
#undef CONDITION_2
#define CONDITION_2 false //Any invocation from this point on wont pass past the logic switch
IF_SWITCH_2(1, 1, 1); //Wont pass
据我所知,更改定义的顺序对错误没有影响。
使用 MinGW 8.10 64 位编译