How is it a keyword and an instance of a type?
This isn't surprising. Both true
and false
are keywords and as literals they have a type ( bool
). nullptr
is a pointer literal of type std::nullptr_t
, and it's a prvalue (you cannot take the address of it using &
).
4.10
about pointer conversion says that a prvalue of type std::nullptr_t
is a null pointer constant, and that an integral null pointer constant can be converted to std::nullptr_t
. The opposite direction is not allowed. This allows overloading a function for both pointers and integers, and passing nullptr
to select the pointer version. Passing NULL
or 0
would confusingly select the int
version.
A cast of nullptr_t
to an integral type needs a reinterpret_cast
, and has the same semantics as a cast of (void*)0
to an integral type (mapping implementation defined). A reinterpret_cast
cannot convert nullptr_t
to any pointer type. Rely on the implicit conversion if possible or use static_cast
.
The Standard requires that sizeof(nullptr_t)
be sizeof(void*)
.
In that code, there doesn't seem to be an advantage. But consider the following overloaded functions:
void f(char const *ptr);
void f(int v);
f(NULL); //which function will be called?
Which function will be called? Of course, the intention here is to call f(char const *)
, but in reality f(int)
will be called! That is a big problem1, isn't it?
So, the solution to such problems is to use nullptr
:
f(nullptr); //first function is called
Of course, that is not the only advantage of nullptr
. Here is another:
template<typename T, T *ptr>
struct something{}; //primary template
template<>
struct something<nullptr_t, nullptr>{}; //partial specialization for nullptr
Since in template, the type of nullptr
is deduced as nullptr_t
, so you can write this:
template<typename T>
void f(T *ptr); //function to handle non-nullptr argument
void f(nullptr_t); //an overload to handle nullptr argument!!!
1. In C++, NULL
is defined as #define NULL 0
, so it is basically int
, that is why f(int)
is called.
Best Answer
Stephan T. Lavavej (member of the C++ standard committee) explained that once in a talk (55:35):
While an implementation is allowed to
#define NULL nullptr
, it would break quite some uses likeand apparently there are plenty of those. So they could not force the change.