diff --git a/book/en-us/02-usability.md b/book/en-us/02-usability.md index f55794e..fa9b2b1 100644 --- a/book/en-us/02-usability.md +++ b/book/en-us/02-usability.md @@ -40,7 +40,7 @@ void foo(char*); void foo(int); ``` -Then the `foo(NULL);` statement will call `foo(int)`, which will cause the code to be counterintuitive. +Then the behavior of the `foo(NULL);` statement depends on how `NULL` is implemented: when `NULL` is defined as `0` (for example, on MSVC), it calls `foo(int)`, which is counterintuitive; when `NULL` is defined as the GCC/Clang builtin `__null`, the call `foo(NULL)` becomes ambiguous between the `char*` and `int` overloads and fails to compile. In either case, `NULL` does not behave the way a proper null pointer should during overload resolution. 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. diff --git a/book/zh-cn/02-usability.md b/book/zh-cn/02-usability.md index 6363366..efde82c 100644 --- a/book/zh-cn/02-usability.md +++ b/book/zh-cn/02-usability.md @@ -29,7 +29,7 @@ void foo(char*); void foo(int); ``` -那么 `foo(NULL);` 这个语句将会去调用 `foo(int)`,从而导致代码违反直觉。 +那么 `foo(NULL);` 这个语句的行为将取决于 `NULL` 的具体实现:当 `NULL` 被定义为 `0` 时(例如 MSVC),它会去调用 `foo(int)`,从而导致代码违反直觉;而当 `NULL` 被定义为 GCC/Clang 的内建常量 `__null` 时,`foo(NULL)` 则会因为 `char*` 与 `int` 两个重载都能匹配而产生二义性,导致编译失败。无论哪种情况,`NULL` 在重载决议中的表现都不符合空指针应有的语义。 为了解决这个问题,C++11 引入了 `nullptr` 关键字,专门用来区分空指针、`0`。而 `nullptr` 的类型为 `nullptr_t`,能够隐式的转换为任何指针或成员指针的类型,也能和他们进行相等或者不等的比较。