Skip to content

Commit bdf0e89

Browse files
book: fix imprecise description of NULL in C++ (#279)
1 parent ff6ee89 commit bdf0e89

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

book/en-us/02-usability.md

+9-8
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,21 @@ which refers to the language behavior that occurred before the runtime.
1818

1919
### nullptr
2020

21-
The purpose of `nullptr` appears to replace `NULL`. In a sense,
22-
traditional C++ treats `NULL` and `0` as the same thing,
23-
depending on how the compiler defines NULL,
24-
and some compilers define NULL as `((void*)0)` Some will define it directly as `0`.
21+
The purpose of `nullptr` appears to replace `NULL`. There are **null pointer constants** in the C and C++ languages,
22+
which can be implicitly converted to null pointer value of any pointer type,
23+
or null member pointer value of any pointer-to-member type in C++.
24+
`NULL` is provided by the standard library implementation and defined as an implementation-defined null pointer constant.
25+
In C, some standard libraries defines `NULL` as `((void*)0)` and some define it as `0`.
2526

26-
C++ **does not allow** to implicitly convert `void *` to other types.
27-
But if the compiler tries to define `NULL` as `((void*)0)`, then in the following code:
27+
C++ **does not allow** to implicitly convert `void *` to other types, and thus `((void*)0)` is not a valid implementation
28+
of `NULL`. If the standard library tries to define `NULL` as `((void*)0)`, then compilation error would occur in the following code:
2829

2930
```cpp
3031
char *ch = NULL;
3132
```
3233

3334
C++ without the `void *` implicit conversion has to define `NULL` as `0`.
34-
This still creates a new problem. Defining `NULL` to 0 will cause the overloading feature in `C++` to be confusing.
35+
This still creates a new problem. Defining `NULL` to `0` will cause the overloading feature in `C++` to be confusing.
3536
Consider the following two `foo` functions:
3637

3738
```cpp
@@ -41,7 +42,7 @@ void foo(int);
4142
4243
Then the `foo(NULL);` statement will call `foo(int)`, which will cause the code to be counterintuitive.
4344
44-
To solve this problem, C++11 introduced the `nullptr` keyword, which is specifically used to distinguish null pointers, 0. The type of `nullptr` is `nullptr_t`, which can be implicitly converted to any pointer or member pointer type, and can be compared equally or unequally with them.
45+
To solve this problem, C++11 introduced the `nullptr` keyword, which is specifically used to distinguish null pointers, `0`. The type of `nullptr` is `nullptr_t`, which can be implicitly converted to any pointer or member pointer type, and can be compared equally or unequally with them.
4546
4647
You can try to compile the following code using clang++:
4748

book/zh-cn/02-usability.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ order: 2
1414

1515
### nullptr
1616

17-
`nullptr` 出现的目的是为了替代 `NULL`在某种意义上来说,传统 C++ 会把 `NULL``0` 视为同一种东西,这取决于编译器如何定义 `NULL`,有些编译器会将 `NULL` 定义为 `((void*)0)`,有些则会直接将其定义为 `0`
17+
`nullptr` 出现的目的是为了替代 `NULL` C 与 C++ 语言中有**空指针常量**,它们能被隐式转换成任何指针类型的空指针值,或 C++ 中的任何成员指针类型的空成员指针值。 `NULL` 由标准库实现提供,并被定义为实现定义的空指针常量。在 C 中,有些标准库会把 `NULL` 定义为 `((void*)0)` 而有些将它定义为 `0`
1818

19-
C++ **不允许**直接将 `void *` 隐式转换到其他类型。但如果编译器尝试把 `NULL` 定义为 `((void*)0)`那么在下面这句代码中
19+
C++ **不允许**直接将 `void *` 隐式转换到其他类型,从而 `((void*)0)` 不是 `NULL` 的合法实现。如果标准库尝试把 `NULL` 定义为 `((void*)0)`那么下面这句代码中会出现编译错误
2020

2121
```cpp
2222
char *ch = NULL;

0 commit comments

Comments
 (0)