#定义空值 空值
#ifndef NULL
#define NULL NULL
#endif
此代码在 gcc 中编译,没有警告/错误。有人可以解释一下预处理器在这里做什么吗?
#ifndef NULL
#define NULL NULL
#endif
This code compiles in gcc with no warnings/errors. Can someone explain what the preprocessor is doing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
编译器在任何地方看到文本“NULL”,都会将其替换为文本“NULL”。这就像在代码中搜索并替换“NULL”并替换为“NULL”。不违法,只是奇怪:)
Anywhere the compiler sees the text "NULL" it will replace it with the text "NULL". It's like doing a search-and-replace in your code for "NULL" and replacing with "NULL". Not illegal, just weird :)
这样做的唯一可能的原因是在包含头文件之前执行此操作,这些头文件本身会执行类似
这样的操作,这样就会阻止 NULL 被这样定义。
The only possible reason for doing this would be to do it before including header files which themselves do something like
This would then stop the NULL from being defined like that.
这很正常:
这也能编译。这是因为预处理器只是进行无意识的替换。它替换的内容以及替换的内容不需要是有效的标识符。
可以这样想:打开编辑器的“搜索”和“搜索”。替换窗口并在
Replace
和Replace with
字段中键入“NULL”。它不会给出任何错误或警告,并且它会“工作”,即使它实际上不执行任何操作。预处理器做同样的事情。显然,当您尝试使用它时:
It's normal:
This compiles too. This is because the preprocessor simply does mindless replaces. What it replaces and what it replaces with don't need to be valid identifiers.
Think of it like this: open your editor's Search & Replace window and type in "NULL" in both the
Replace
andReplace with
fields. It won't give any error or warning and it will "work", even though it actually doesn't do anything. The preprocessor does the same thing.Obviously when you try to use it:
我见过类似这样的代码将值从编译器命名空间(通常是“命名空间”,而不是 C++
命名空间
)带到预处理器命名空间的情况,例如:真的很难看东西。
就我个人而言,我还没有发现这种东西的用途,但仍然存在。
编辑:虽然我已经看到了这个,但我不知道为什么它会有用,除了检查“
FOO
”是否在其他地方定义为预处理器符号代码;也许是在处理一些遗留代码。有人吗?I've seen cases where code like this brings a value from the compiler namespace ("namespace" in general, not C++
namespace
) to the preprocessor namespace, e.g.:Really ugly stuff.
Personally I haven't found a use for this sort of thing, but exists nonetheless.
EDIT: While I've seen this, I don't know why it would be useful, other than check if "
FOO
" defined as a preprocessor symbol somewhere else in the code; perhaps in dealing with some legacy code. Anyone?显然,这不是用作宏,而是用作编译标志。您是否在代码的其他区域看到
#ifdef NULL
或#ifndef NULL
?使用“NULL”是非常奇怪的,特别是作为这样的标志,但我见过陌生人(
#define TRUE FALSE
)...Clearly this isn't being used as a macro, it's being used as a compilation flag. Are there other areas of the code where you see
#ifdef NULL
or#ifndef NULL
?It is very strange to use "NULL", specifically, as such a flag, but I've seen stranger (
#define TRUE FALSE
)...回答预处理器正在做什么的问题:
除非前面有 undefs NULL 的代码,否则它会完全跳过
#define NULL NULL
。几乎可以肯定 NULL 已经被定义了。在 C++ 中,由于 C++ 具有更严格的类型检查,因此首选使用 0。如果必须使用 NULL,最好声明const int NULL = 0;
(参见 Stroustrup 的第 5.1.1 节)。In answer to the question of what the preprocessor is doing:
Unless there's preceding code that undefs NULL, it's skipping the
#define NULL NULL
completely. NULL is almost certainly already defined. In C++, the use of 0 is preferred due to C++'s tighter type-checking. If you must use NULL, it's best to declareconst int NULL = 0;
(see Section 5.1.1 of Stroustrup).这不是简单地将 NULL 定义为字符序列“NULL”吗?
doesn't that simply define NULL as the character sequence "NULL" ?