Which one should I use? Any advantages if I use one over the other?
-
3`nullptr_t` is the type of `nullptr` – OMGtechy Jun 24 '15 at 20:18
-
3`nullptr` and `nullptr_t` are never interchangeable. The question makes no sense. There's no such matter as choosing which one to use. – AnT stands with Russia Jun 24 '15 at 20:23
-
There was a [very good answer](http://qr.ae/74om57) on the same question submitted on Quora earlier today by Brain. Not sure if you're the one who asked it as it seems very coincidental. – David G Jun 24 '15 at 20:25
7 Answers
nullptr
is the constant, nullptr_t
is its type. Use each one in contexts where you need respectively a null pointer, or the type of a null pointer.

- 62,093
- 7
- 131
- 191
"... 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
is (1)effectively a constant of type nullptr_t
that represents a specific compiler implementation defined value.
See the C++11 standards section:
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) Just like the this
keyword nullptr
stands for an rvalue rather than being of const
type. Thus, decltype(nullptr)
can be a non-const
type. With Visual C++ 2015 and MinGW g++ 5.1 it is non-const
.

- 142,714
- 15
- 209
- 331

- 1
- 13
- 116
- 190
In exactly the same way that true
is a C++ keyword literal of type bool
, nullptr
is a C++ keyword literal of type std::nullptr_t
.

- 151
- 3
If you try this
cout << typeid(nullptr).name() << endl;
you will see that nullptr is of type std::nullptr_t.

- 353
- 1
- 3
- 10
nullptr
is a pointer literal of type std::nullptr_t
.
And moreover nullptr
is also a keyword of the C++ the same way as boolean literals false and true.:)

- 301,070
- 26
- 186
- 335
From [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 ]
So use nullptr
when you need a pointer literal, and std::nullptr_t
in a context when you need to take that type. The latter, for instance, if you're making a function or constructor or something that can take a nullptr
as an argument.

- 286,269
- 29
- 621
- 977