空指针常量 (nullptr)、空指针值和空成员指针值之间有什么区别?
What is the difference between a null pointer constant (nullptr), a null pointer value and a null member pointer value?
在 ISO/IEC 14882:2017 (C++17) 中,第 5.13.7 节 "Pointer literals" 规定:
5.13.7 Pointer literals [lex.nullptr]
pointer-literal: nullptr
1 The pointer literal is the keyword nullptr. It is a prvalue of type std::nullptr_t. [Note: std::nullptr_t is a distinct type that is neither a pointer type nor a pointer to member type; rather, a prvalue of this type is a null pointer constant and can be converted to a null pointer value or null member pointer value. See 7.11 and 7.12. —end note]
以下,nullptr
是 std::nullptr_t
类型的纯右值。 std::nullptr_t
类型的纯右值是空指针常量;因此 nullptr
是一个空指针常量。空指针常量(nullptr
也是如此)可以转换为空指针值或空成员指针值。
现在我在ISO/IEC14882:2017的"Pointer conversions"中引用了Section 7.11:
A null pointer constant is an integer literal (5.13.2) with value zero or a prvalue of type std::nullptr_t. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of object pointer or function pointer type.
我知道空指针常量是一个值为零的整型文字或 std::nullptr_t
类型的纯右值,但我不明白空指针值和空成员指针值的区别.我不明白从空指针常量到指针类型的转换结果如何是 "null pointer value of that type" 以及该上下文中的空指针值是什么。
我的问题是:
- 空指针常量(
nullptr
)、空指针值和空成员指针值有什么区别?
What is the difference between a null pointer constant (nullptr), a null pointer value and a null member pointer value?
空指针常量是 nullptr(或 std::nullptr_t
类型的任何其他纯右值),或值为 0 的整数文字。空指针常量示例:
NULL // a macro for one of the others
0
0L
nullptr
std::nullptr_t fun();
fun() // also a null pointer constant
其中,除非您需要支持早于 C++11 的标准,否则不要使用整数文字或 NULL。
空指针值是表示空值的指针类型的值。空指针值的例子:
(void*)nullptr // pointer to void
(int*)nullptr // pointer to object
using void_fun = void();
(void_fun*)nullptr // pointer to function
Null成员指针值是表示null的成员指针类型的值。空成员指针值示例:
(int some_class::*)nullptr // pointer to data member
using mem_void_fun_ptr = void(some_class::*)(); // unfortunately cannot alias member
// function type as far as I know,
// so we alias pointer to it instead
(mem_void_fun_ptr)nullptr // pointer to member function
注意 std::nullptr_t
不是指针类型,也不是指向成员类型的指针。
在 ISO/IEC 14882:2017 (C++17) 中,第 5.13.7 节 "Pointer literals" 规定:
5.13.7 Pointer literals [lex.nullptr]
pointer-literal: nullptr
1 The pointer literal is the keyword nullptr. It is a prvalue of type std::nullptr_t. [Note: std::nullptr_t is a distinct type that is neither a pointer type nor a pointer to member type; rather, a prvalue of this type is a null pointer constant and can be converted to a null pointer value or null member pointer value. See 7.11 and 7.12. —end note]
以下,nullptr
是 std::nullptr_t
类型的纯右值。 std::nullptr_t
类型的纯右值是空指针常量;因此 nullptr
是一个空指针常量。空指针常量(nullptr
也是如此)可以转换为空指针值或空成员指针值。
现在我在ISO/IEC14882:2017的"Pointer conversions"中引用了Section 7.11:
A null pointer constant is an integer literal (5.13.2) with value zero or a prvalue of type std::nullptr_t. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of object pointer or function pointer type.
我知道空指针常量是一个值为零的整型文字或 std::nullptr_t
类型的纯右值,但我不明白空指针值和空成员指针值的区别.我不明白从空指针常量到指针类型的转换结果如何是 "null pointer value of that type" 以及该上下文中的空指针值是什么。
我的问题是:
- 空指针常量(
nullptr
)、空指针值和空成员指针值有什么区别?
What is the difference between a null pointer constant (nullptr), a null pointer value and a null member pointer value?
空指针常量是 nullptr(或 std::nullptr_t
类型的任何其他纯右值),或值为 0 的整数文字。空指针常量示例:
NULL // a macro for one of the others
0
0L
nullptr
std::nullptr_t fun();
fun() // also a null pointer constant
其中,除非您需要支持早于 C++11 的标准,否则不要使用整数文字或 NULL。
空指针值是表示空值的指针类型的值。空指针值的例子:
(void*)nullptr // pointer to void
(int*)nullptr // pointer to object
using void_fun = void();
(void_fun*)nullptr // pointer to function
Null成员指针值是表示null的成员指针类型的值。空成员指针值示例:
(int some_class::*)nullptr // pointer to data member
using mem_void_fun_ptr = void(some_class::*)(); // unfortunately cannot alias member
// function type as far as I know,
// so we alias pointer to it instead
(mem_void_fun_ptr)nullptr // pointer to member function
注意 std::nullptr_t
不是指针类型,也不是指向成员类型的指针。