[英]What is the difference between nullptr and nullptr_t in C++?
Which one should I use? 我应该使用哪一个? Any advantages if I use one over the other? 如果我使用一个而不是另一个优势?
nullptr
is the constant, nullptr_t
is its type. nullptr
是常量, nullptr_t
是它的类型。 Use each one in contexts where you need respectively a null pointer, or the type of a null pointer. 在需要分别使用空指针或空指针类型的上下文中使用每一个。
与true
std::nullptr_t
bool
类型的C ++关键字文字的方式完全相同, nullptr
是类型为std::nullptr_t
的C ++关键字文字。
nullptr
的类型为nullptr_t
。
"... if I use one over the other?" “......如果我用一个而不是另一个?”
You can't ( use one over the other ) they're orthogonal by these means: 你不能( 使用一个在另一个上 )通过以下方式正交:
nullptr_t
is the type used to represent a nullptr
nullptr_t
是用于表示nullptr
的类型
nullptr
is (1) effectively a constant of type nullptr_t
that represents a specific compiler implementation defined value. nullptr
是(1)有效的类型的恒定 nullptr_t
代表特定编译器实现定义的值。
See the C++11 standards section: 请参阅C ++ 11标准部分:
2.14.7 Pointer literals 2.14.7指针文字
- The pointer literal is the keyword
nullptr
. 指针文字是关键字nullptr
。 It is a prvalue of typestd::nullptr_t
. 它是std::nullptr_t
类型的prvalue。
[ Note:std::nullptr_t
is a distinct type that is neither a pointer type nor a pointer to member type; [注意:std::nullptr_t
是一个不同的类型,既不是指针类型也不是指向成员类型的指针; 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. 相反,这种类型的prvalue是一个空指针常量,可以转换为空指针值或null成员指针值。 See 4.10 and 4.11. 见4.10和4.11。 — end note ] - 结束说明]
1) Just like the this
keyword nullptr
stands for an rvalue rather than being of const
type. 1)就像this
关键字一样, nullptr
代表rvalue而不是const
类型。 Thus, decltype(nullptr)
can be a non- const
type. 因此, decltype(nullptr)
可以是非const
类型。 With Visual C++ 2015 and MinGW g++ 5.1 it is non- const
. 用Visual C ++ 2015和MinGW克++ 5.1它是非const
。
nullptr
is a pointer literal of type std::nullptr_t
. nullptr
是std::nullptr_t
类型的指针文字。 And moreover nullptr
is also a keyword of the C++ the same way as boolean literals false and true.:) 而且nullptr
也是C ++的关键字,与布尔文字false和true相同。:)
From [lex.nullptr]: 来自[lex.nullptr]:
Pointer Literals 指针文字
pointer-literal : 指针文字 :
nullptr
The pointer literal is the keyword
nullptr
. 指针文字是关键字nullptr
。 It is a prvalue of typestd::nullptr_t
. 它是std::nullptr_t
类型的prvalue。 [ Note:std::nullptr_t
is a distinct type that is neither a pointer type nor a pointer to member type; [注意:std::nullptr_t
是一个不同的类型,既不是指针类型也不是指向成员类型的指针; 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. 相反,这种类型的prvalue是一个空指针常量,可以转换为空指针值或null成员指针值。 See 4.10 and 4.11. 见4.10和4.11。 —end note ] - 尾注]
So use nullptr
when you need a pointer literal, and std::nullptr_t
in a context when you need to take that type. 因此,当您需要指针文字时使用nullptr
,并在需要采用该类型时在上下文中使用std::nullptr_t
。 The latter, for instance, if you're making a function or constructor or something that can take a nullptr
as an argument. 例如,后者,如果你正在创建一个函数或构造函数或者可以将nullptr
作为参数的东西。
If you try this 如果你试试这个
cout << typeid(nullptr).name() << endl;
you will see that nullptr is of type std::nullptr_t. 你会看到nullptr的类型为std :: nullptr_t。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.