什么头文件在 C++ 中定义了 NULL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12023476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:47:03  来源:igfitidea点击:

What header defines NULL in C++?

c++null

提问by James Leonard

According to C++ primer, <cstdlib>header defines NULL. cpluspplussays it is defined in <cstddef>.

根据C++ 入门<cstdlib>标题定义了NULL. cpluspplus说它是在<cstddef>.

Ultimately, if the right header is not included, I thought NULLcan't be referenced.

最终,如果不包含正确的标题,我认为NULL无法引用。

From what i can see, however it can be referenced and produce programs and that compile and run without warnings or errors, after including only <iostream>

从我所看到的,但是它可以被引用并生成程序并且编译和运行没有警告或错误,只包括 <iostream>

Please help me understand this.

请帮助我理解这一点。

采纳答案by Vaughn Cato

C++03 section 18.1.2 says that NULL is defined in cstddef.

C++03 第 18.1.2 节说 NULL 是在 cstddef 中定义的。

On some implementations, iostream may include cstddef, so including iostream would also give you NULL.

在某些实现中,iostream 可能包含 cstddef,因此包含 iostream 也会为您提供 NULL。

回答by Jerry Coffin

The C standard requires that NULL be defined in locale.h, stddef.h, stdio.h, stdlib.h, string.h, time.h, and wchar.h.

C标准要求NULL中定义locale.hstddef.hstdio.hstdlib.hstring.htime.h,和wchar.h

The C++ standard requires that NULL be defined in the c*header corresponding to each of those.

C++ 标准要求在c*对应于每一个的标头中定义 NULL 。

The C standard is very strict about the names a standard can define--each standard header must define precisely the names the standard requires that header to define. The only other names it can define are those that are reserved for the implementation, such as those starting with an underscore followed by another underscore or a capital letter.

C 标准对标准可以定义的名称非常严格——每个标准头文件必须精确定义标准要求该头文件定义的名称。它可以定义的唯一其他名称是为实现保留的名称,例如以下划线开头的名称,后跟另一个下划线或大写字母的名称。

The C++ standard is much more permissive in this respect--including any one standard header can have the same effect as including any or all other standard headers.

C++ 标准在这方面更加宽容——包括任何一个标准头文件都可以与包括任何或所有其他标准头文件具有相同的效果。

From a practical viewpoint, C++ implementations used to take quite a bit of advantage of this permissiveness--that is, including one standard header frequently defined the names from a number of other standard headers. More recent implementations tend to work more like the C standard requires, staying much closer to each header defining only the names required by to be defined by that header. They're still probably not as strict about it as the C standard requires, but much closer than they used to be (as a rule).

从实际的角度来看,C++ 实现过去常常利用这种宽容性——也就是说,包括一个标准头文件,经常定义来自许多其他标准头文件的名称。最近的实现往往更像 C 标准要求的那样工作,更接近每个头文件,只定义由该头文件定义的名称。他们可能仍然没有像 C 标准要求的那样严格,但比以前更接近(通常)。

回答by R Sahu

The C++11 Standard says NULLmust be defined in multiple files. They are:

C++11 标准NULL规定必须在多个文件中定义。他们是:

<clocale>
<cstddef>
<cstdlib>
<cstring>
<ctime>
<cwchar>

This is mentioned in Table 149, Section C.3 C standard library/3of the standard.

这在标准的表 149,C.3C 标准库/3中提到。

Here's an image of the table and some surrounding text.

这是表格的图像和一些周围的文字。

enter image description here

在此处输入图片说明

回答by Cornstalks

It is defined in <cstddef>

它定义在 <cstddef>

The C++11 standard section 18.2, table 30 explains what's in <cstddef>It says:

C++11 标准第 18.2 节,表 30 解释了其中的内容<cstddef>它说:

Table 30 — Header <cstddef>synopsis

Macros: NULL[...]

[...] The macro NULL is an implementation-de?ned C++ null pointer constant in this International Standard

表 30 — 标题<cstddef>概要

宏:NULL[...]

[...] 宏 NULL 是此国际标准中实现定义的 C++ 空指针常量

回答by Arshia Aghaei

In the latest version of MinGW (MinGW 5.3.0) NULL was defined in the wchar.h header as :

在最新版本的 MinGW (MinGW 5.3.0) NULL 在 wchar.h 标头中定义为:

#define NULL 0