宏定义中的令牌未在此范围内声明
Token in macro definition was not declared in this scope
我正在尝试使用定义宏来创建一个逻辑系统来实现我的记录器,当定义某些切换时,该系统将展开为空。问题是,当我将多个这些开关堆叠在一起时,一个嵌套在另一个内部(就像调用 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 位编译
What causes these errors?
展开一次运行kind-of。一旦一个宏被展开,它就不会再被展开,在嵌套的宏调用中也是如此。不管怎样链是:
IF_SWITCH_1(1, 1, 1)
_LOGIC_SWITCH_(IF_SWITCH_1_, CONDITION_1, 1, 1, 1) // step 2
_ADD_PARTS(IF_SWITCH_1_, true, 1, 1, 1)
_ADD_PARTS2(IF_SWITCH_1_, true, 1, 1, 1)
IF_SWITCH_1_true(1, 1, 1)
IF_SWITCH_2(1, 1, 1)
_LOGIC_SWITCH_(IF_SWITCH_2_, CONDITION_1, 1, 1, 1)
// _LOGIC_SWITCH_ was expanded at step #2
_LOGIC_SWITCH_(IF_SWITCH_2_, true, 1, 1, 1)
// expanding stops here
How could I fix them?
首先,带有前导下划线后跟大写字母的标识符被保留用于实现。不要在您自己的代码中使用此类标识符。
如果您希望运行时间评价,我建议:
static inline void if_switch_2_execute(int a, int b, int c) {
std::cout << "passed" << std::endl;
}
#define IF_SWITCH_2(a, b, c) (CONDITION_2?if_switch_2_execute(a, b, c):(void)0)
#define IF_SWITCH_1(a, b, c) (CONDITION_1?IF_SWITCH_2(a, b, c):(void)0)
无论如何,解决方法是将 _LOGIC_SWITCH_
的求值从 _LOGIC_SWITCH_
内的嵌套调用移动到上层,以便 _LOGIC_SWITCH_
在一个求值链中仅展开一次(我不知道如何解释它,这就是我的理解......:/)。这就是为什么通常 #define MACRO(something) CHOOSE_FUNCTION_TO_CALL(__VA_ARGS__)(__VA_ARGS__)
.
#define CONDITION_1 true
#define CONDITION_2 true
#define CONCAT(a, b) a##b
#define XCONCAT(a, b) CONCAT(a, b) // this is your _LOGIC_SWITCH_
#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) XCONCAT(IF_SWITCH_2_, CONDITION_2)(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) XCONCAT(IF_SWITCH_1_, CONDITION_1)(a, b, c)
我正在尝试使用定义宏来创建一个逻辑系统来实现我的记录器,当定义某些切换时,该系统将展开为空。问题是,当我将多个这些开关堆叠在一起时,一个嵌套在另一个内部(就像调用 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 位编译
What causes these errors?
展开一次运行kind-of。一旦一个宏被展开,它就不会再被展开,在嵌套的宏调用中也是如此。不管怎样链是:
IF_SWITCH_1(1, 1, 1)
_LOGIC_SWITCH_(IF_SWITCH_1_, CONDITION_1, 1, 1, 1) // step 2
_ADD_PARTS(IF_SWITCH_1_, true, 1, 1, 1)
_ADD_PARTS2(IF_SWITCH_1_, true, 1, 1, 1)
IF_SWITCH_1_true(1, 1, 1)
IF_SWITCH_2(1, 1, 1)
_LOGIC_SWITCH_(IF_SWITCH_2_, CONDITION_1, 1, 1, 1)
// _LOGIC_SWITCH_ was expanded at step #2
_LOGIC_SWITCH_(IF_SWITCH_2_, true, 1, 1, 1)
// expanding stops here
How could I fix them?
首先,带有前导下划线后跟大写字母的标识符被保留用于实现。不要在您自己的代码中使用此类标识符。
如果您希望运行时间评价,我建议:
static inline void if_switch_2_execute(int a, int b, int c) {
std::cout << "passed" << std::endl;
}
#define IF_SWITCH_2(a, b, c) (CONDITION_2?if_switch_2_execute(a, b, c):(void)0)
#define IF_SWITCH_1(a, b, c) (CONDITION_1?IF_SWITCH_2(a, b, c):(void)0)
无论如何,解决方法是将 _LOGIC_SWITCH_
的求值从 _LOGIC_SWITCH_
内的嵌套调用移动到上层,以便 _LOGIC_SWITCH_
在一个求值链中仅展开一次(我不知道如何解释它,这就是我的理解......:/)。这就是为什么通常 #define MACRO(something) CHOOSE_FUNCTION_TO_CALL(__VA_ARGS__)(__VA_ARGS__)
.
#define CONDITION_1 true
#define CONDITION_2 true
#define CONCAT(a, b) a##b
#define XCONCAT(a, b) CONCAT(a, b) // this is your _LOGIC_SWITCH_
#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) XCONCAT(IF_SWITCH_2_, CONDITION_2)(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) XCONCAT(IF_SWITCH_1_, CONDITION_1)(a, b, c)