C++ 中的 nullptr 和 nullptr_t 有什么区别?
What is the difference between nullptr and nullptr_t in C++?
我应该使用哪一个?如果我使用一个比另一个有什么优势?
nullptr
属于 nullptr_t
.
类型
nullptr
是常量,nullptr_t
是它的类型。在分别需要空指针或空指针类型的上下文中使用每一个。
如果你试试这个
cout << typeid(nullptr).name() << endl;
你会看到 nullptr 的类型是 std::nullptr_t.
nullptr
是 std::nullptr_t
类型的指针文字。
而且 nullptr
也是 C++ 的关键字,就像布尔文字 false 和 true 一样。:)
来自[lex.nullptr]:
Pointer Literals
pointer-literal:
nullptr
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 4.10
and 4.11. —end note ]
因此,当您需要指针文字时使用 nullptr
,而当您需要采用该类型时,在上下文中使用 std::nullptr_t
。后者,例如,如果你正在制作一个函数或构造函数或可以将 nullptr
作为参数的东西。
"... if I use one over the other?"
你不能(用一个代替另一个)它们通过这些方式是正交的:
nullptr_t
是 类型 用来表示 nullptr
nullptr
是 (1) 实际上是 常量 类型 nullptr_t
表示特定的编译器实现定义值。
参见 C++11 standards 部分:
2.14.7 Pointer literals
- 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 4.10
and 4.11. — end note ]
1) 就像 this
关键字一样 nullptr
代表右值而不是 const
类型。因此,decltype(nullptr)
可以是非 const
类型。对于 Visual C++ 2015 和 MinGW g++ 5.1,它是非 const
。
与 true
是类型 bool
的 C++ 关键字字面量完全相同,nullptr
是类型 std::nullptr_t
.[=14 的 C++ 关键字字面量=]
我应该使用哪一个?如果我使用一个比另一个有什么优势?
nullptr
属于 nullptr_t
.
nullptr
是常量,nullptr_t
是它的类型。在分别需要空指针或空指针类型的上下文中使用每一个。
如果你试试这个
cout << typeid(nullptr).name() << endl;
你会看到 nullptr 的类型是 std::nullptr_t.
nullptr
是 std::nullptr_t
类型的指针文字。
而且 nullptr
也是 C++ 的关键字,就像布尔文字 false 和 true 一样。:)
来自[lex.nullptr]:
Pointer Literals
pointer-literal:
nullptr
The pointer literal is the keyword
nullptr
. It is a prvalue of typestd::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 4.10 and 4.11. —end note ]
因此,当您需要指针文字时使用 nullptr
,而当您需要采用该类型时,在上下文中使用 std::nullptr_t
。后者,例如,如果你正在制作一个函数或构造函数或可以将 nullptr
作为参数的东西。
"... if I use one over the other?"
你不能(用一个代替另一个)它们通过这些方式是正交的:
nullptr_t
是 类型 用来表示 nullptr
nullptr
是 (1) 实际上是 常量 类型 nullptr_t
表示特定的编译器实现定义值。
参见 C++11 standards 部分:
2.14.7 Pointer literals
- The pointer literal is the keyword
nullptr
. It is a prvalue of typestd::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 4.10 and 4.11. — end note ]
1) 就像 this
关键字一样 nullptr
代表右值而不是 const
类型。因此,decltype(nullptr)
可以是非 const
类型。对于 Visual C++ 2015 和 MinGW g++ 5.1,它是非 const
。
与 true
是类型 bool
的 C++ 关键字字面量完全相同,nullptr
是类型 std::nullptr_t
.[=14 的 C++ 关键字字面量=]