是否可以在 C/C++ 中嵌套预处理器 #define
(在本例中为 #ifdef
)?我希望能够做到这一点:
#ifdef _DEBUG_
#define __DEV_MODE_VERBOSE__ // Defined in DEBUG mode only
#endif
这个想法是“__DEV_MODE_VERBOSE__
”通常会被注释掉。取消注释时,我可以使用类似的东西:
#ifdef __DEV_MODE_VERBOSE__
int test = (5 * GetTickCount()); // Not reached!
#endif // __DEV_MODE_VERBOSE__
记录通过我的通信链接发送和接收的命令。但是,如果 #define
被意外地留下未注释,日志记录将不会出现在 RELEASE(因此生产)代码中。
当我尝试像上面那样定义“__DEV_MODE_VERBOSE__
”时,它没有被定义,所以我不确定是语法不正确还是问题更根本。如果我不在“#ifdef _DEBUG_
”及其“#endif
”之间嵌套,则符号是。
最佳答案
N3337 [global.names]:
Certain sets of names and function signatures are always reserved to the implementation:— Each name that contains a double underscore
__
or begins with an underscore followed by an uppercase letter is reserved to the implementation for any use.— Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace.
您的宏包含双下划线,因此保留给实现。将其更改为有效名称应该可以解决您的问题。
关于c++ - 基于另一个已定义符号的条件#define,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32520257/