c++ - 错误 : macro names must be identifiers using #ifdef 0

标签 c++ macros c-preprocessor

我有一个用 C++ 编写的应用程序的源代码,我只想用以下方式评论一些东西:

#ifdef 0
...
#endif

我得到了这个错误

error: macro names must be identifiers

为什么会这样?

最佳答案

#ifdef 指令用于检查是否定义了预处理器符号。标准(C11 6.4.2 标识符)规定标识符不得以数字开头:

identifier:
    identifier-nondigit
    identifier identifier-nondigit
    identifier digit
identifier-nondigit:
    nondigit
    universal-character-name
    other implementation-defined characters>
nondigit: one of
    _ a b c d e f g h i j k l m
    n o p q r s t u v w x y z
    A B C D E F G H I J K L M
    N O P Q R S T U V W X Y Z
digit: one of
    0 1 2 3 4 5 6 7 8 9

使用预处理器阻塞代码的正确形式是:

#if 0
: : :
#endif

你也可以使用:

#ifdef NO_CHANCE_THAT_THIS_SYMBOL_WILL_EVER_EXIST
: : :
#endif

但您需要确信这些符号不会被您自己的代码以外的代码无意中设置。换句话说,不要使用其他人也可能使用的 NOTUSEDDONOTCOMPILE 之类的东西。为了安全起见,应该首选 #if 选项。

关于c++ - 错误 : macro names must be identifiers using #ifdef 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/426736/

相关文章:

带有定义或内联的 C++ 宏?

c - 从 CMake 检索宏

c++ - 在 Windows 中通过 C++ 更改另一个程序的变量

c - 编写 C 宏

c - 当括号导致语法错误时,如何在宏参数内的大括号内使用逗号?

c - 何时使用预处理器在 C 中定义函数?

使用 self 的 C 结构

c++ - 接收(recv)完整请求(例如curl HTTP)

c++错误错误C2512没有合适的默认构造函数可用

c++ - C 和 C++ : Partial initialization of automatic structure