c++ - 基于另一个已定义符号的条件#define

标签 c++ c-preprocessor

是否可以在 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/

相关文章:

c++ - 在 C++ 中将字符串转换为枚举?

c++ - 为什么 C/C++ 预处理器在这里添加一个空格?

c# - #define 宏的 C 到 C# 转换

C 预处理器删除尾随逗号

java - 停止 C 进程时出现弹出窗口

c++ - 检查进程是否正在运行 - Windows

c-preprocessor - 关于 C 中的## 预处理器

c - 宏元编程

c++ - 将 'wchar_t*' 输出到 'ofstream'

c++ - 从 C++ 中的 .csv 文件中读取 double