#定义空值 空值

发布于 2024-08-15 17:22:18 字数 112 浏览 3 评论 0原文

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

残花月 2024-08-22 17:22:18

编译器在任何地方看到文本“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 :)

凤舞天涯 2024-08-22 17:22:18

这样做的唯一可能的原因是在包含头文件之前执行此操作,这些头文件本身会执行类似

#ifndef NULL
#define NULL (void *)0
#endif

这样的操作,这样就会阻止 NULL 被这样定义。

The only possible reason for doing this would be to do it before including header files which themselves do something like

#ifndef NULL
#define NULL (void *)0
#endif

This would then stop the NULL from being defined like that.

纵情客 2024-08-22 17:22:18

这很正常:

#ifndef JON_SKEET
#define JON_SKEET JON_SKEET
#endif

这也能编译。这是因为预处理器只是进行无意识的替换。它替换的内容以及替换的内容不需要是有效的标识符。

可以这样想:打开编辑器的“搜索”和“搜索”。替换窗口并在 ReplaceReplace with 字段中键入“NULL”。它不会给出任何错误或警告,并且它会“工作”,即使它实际上不执行任何操作。预处理器做同样的事情。

显然,当您尝试使用它时:

'JON_SKEET' undeclared (first use in this function)
(Each undeclared identifier is reported only once
for each function it appears in.)

It's normal:

#ifndef JON_SKEET
#define JON_SKEET JON_SKEET
#endif

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 and Replace 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:

'JON_SKEET' undeclared (first use in this function)
(Each undeclared identifier is reported only once
for each function it appears in.)
谁的年少不轻狂 2024-08-22 17:22:18

我见过类似这样的代码将值从编译器命名空间(通常是“命名空间”,而不是 C++ 命名空间)带到预处理器命名空间的情况,例如

// In the compiler namespace, not in the preprocessor namespace
static int const FOO = 1234;

// Bring the constant into the preprocessor namespace as well
#ifndef FOO       // <---- FOO really is undefined here.
#define FOO FOO
#endif

真的很难看东西。

就我个人而言,我还没有发现这种东西的用途,但仍然存在。


编辑:虽然我已经看到了这个,但我不知道为什么它会有用,除了检查“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.:

// In the compiler namespace, not in the preprocessor namespace
static int const FOO = 1234;

// Bring the constant into the preprocessor namespace as well
#ifndef FOO       // <---- FOO really is undefined here.
#define FOO FOO
#endif

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?

小霸王臭丫头 2024-08-22 17:22:18

显然,这不是用作宏,而是用作编译标志。您是否在代码的其他区域看到 #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)...

眼眸印温柔 2024-08-22 17:22:18

回答预处理器正在做什么的问题:

除非前面有 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 declare const int NULL = 0; (see Section 5.1.1 of Stroustrup).

烂柯人 2024-08-22 17:22:18

这不是简单地将 NULL 定义为字符序列“NULL”吗?

doesn't that simply define NULL as the character sequence "NULL" ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文