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*).

Answer from Johannes Schaub - litb on Stack Overflow
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › extensions › nullptr-cpp-component-extensions
nullptr (C++/CLI and C++/CX) | Microsoft Learn
June 25, 2025 - pMyClass == nullptr pMyClass == 0 pMyClass == nullptr pMyClass == 0 · The following code example shows that nullptr is interpreted as a handle to any type or a native pointer to any type. In case of function overloading with handles to different types, an ambiguity error will be generated.
Top answer
1 of 15
460

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*).

2 of 15
141

Why nullptr in C++11? What is it? Why is NULL not sufficient?

C++ expert Alex Allain says it perfectly here (my emphasis added in bold):

...imagine you have the following two function declarations:

void func(int n); 
void func(char *s);
 
func( NULL ); // guess which function gets called?

Although it looks like the second function will be called--you are, after all, passing in what seems to be a pointer--it's really the first function that will be called! The trouble is that because NULL is 0, and 0 is an integer, the first version of func will be called instead. This is the kind of thing that, yes, doesn't happen all the time, but when it does happen, is extremely frustrating and confusing. If you didn't know the details of what is going on, it might well look like a compiler bug. A language feature that looks like a compiler bug is, well, not something you want.

Enter nullptr. In C++11, nullptr is a new keyword that can (and should!) be used to represent NULL pointers; in other words, wherever you were writing NULL before, you should use nullptr instead. It's no more clear to you, the programmer, (everyone knows what NULL means), but it's more explicit to the compiler, which will no longer see 0s everywhere being used to have special meaning when used as a pointer.

Allain ends his article with:

Regardless of all this--the rule of thumb for C++11 is simply to start using nullptr whenever you would have otherwise used NULL in the past.

(My words):

Lastly, don't forget that nullptr is an object--a class. It can be used anywhere NULL was used before, but if you need its type for some reason, it's type can be extracted with decltype(nullptr), or directly described as std::nullptr_t, which is simply a typedef of decltype(nullptr), as shown here:

Defined in header <cstddef>:

See:

  1. https://en.cppreference.com/w/cpp/types/nullptr_t
  2. and https://en.cppreference.com/w/cpp/header/cstddef
namespace std
{
typedef decltype(nullptr) nullptr_t; // (since C++11)
// OR (same thing, but using the C++ keyword `using` instead of the C and C++ 
// keyword `typedef`):
using nullptr_t = decltype(nullptr); // (since C++11)
} // namespace std

References:

  1. Cprogramming.com: Better types in C++11 - nullptr, enum classes (strongly typed enumerations) and cstdint
  2. https://en.cppreference.com/w/cpp/language/decltype
  3. https://en.cppreference.com/w/cpp/types/nullptr_t
  4. https://en.cppreference.com/w/cpp/header/cstddef
  5. https://en.cppreference.com/w/cpp/keyword/using
  6. https://en.cppreference.com/w/cpp/keyword/typedef
🌐
EDUCBA
educba.com › home › software development › software development tutorials › c ++ programming tutorial › c++ nullptr
C++ nullptr | How nullptr works in C++ with Examples?
April 18, 2023 - In the second example, we can compare the null and nullptr difference the null value used in the demo() function it shows error and the function which we called it’s as the nullptr like deom1(nullptr) to demo5(nullptr) it showed the output and also the nullptr memory address reference is displayed on the output screen.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
HackerNoon
hackernoon.com › what-exactly-is-nullptr-in-c-94d63y6t
What Exactly Is nullptr in C++ | HackerNoon
April 25, 2020 - NULL is 0(zero) i.e. integer constant zero with C-style typecast to void*, while nullptr is prvalue of type nullptr_t which is integer literal evaluates to zero.
🌐
FavTutor
favtutor.com › blogs › nullptr-cpp
What is nullptr in C++? Advantages, Use Cases & Examples
May 2, 2023 - int* ptr = nullptr; if (ptr == nullptr) { // Do something if ptr is a null pointer } With the == operator, we are determining in this example whether ptr is equal to nullptr.
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › cpp › nullptr
nullptr | Microsoft Learn
August 3, 2021 - Avoid using NULL or zero (0) as ... in most situations. For example, given func(std::pair<const char *, double>), then calling func(std::make_pair(NULL, 3.14)) causes a compiler error....
🌐
Cppreference
en.cppreference.com › w › cpp › language › nullptr.html
nullptr, the pointer literal (since C++11) - cppreference.com
#include <cstddef> #include <iostream> template<class T> constexpr T clone(const T& t) { return t; } void g(int*) { std::cout << "Function g called\n"; } int main() { g(nullptr); // Fine g(NULL); // Fine g(0); // Fine g(clone(nullptr)); // Fine // g(clone(NULL)); // ERROR: non-literal zero cannot be a null pointer constant // g(clone(0)); // ERROR: non-literal zero cannot be a null pointer constant }
🌐
Scaler
scaler.com › home › topics › what is nullptr in c++?
What is nullptr in C++? - Scaler Topics
May 4, 2023 - The nullptr is an elusive example of a Return type Resolver idiom to deduce the correct type of null pointer which depend upon the type of the instance it is assigned to.
Find elsewhere
🌐
Codecademy
codecademy.com › docs › c++ › pointers › nullptr
C++ (C Plus Plus) | Pointers | nullptr | Codecademy
November 20, 2024 - In this example, nullptr ensures the pointer is safely initialized and easily checked, reducing the risk of undefined behavior from uninitialized pointers.
🌐
Medium
medium.com › @weidagang › modern-c-nullptr-fa494808d31a
Modern C++: nullptr. Saying Goodbye to NULL | by Dagang Wei | Medium
June 24, 2024 - int* ptr = nullptr; // Clear indication of a null pointer · Here’s an example that demonstrates the unique nature of std::nullptr_t:
🌐
IBM
ibm.com › docs › en › zos › 2.5.0
Null pointers - IBM Documentation
April 28, 2023 - For example, a null pointer constant can be 0, 0L, or such an expression that can be cast to type (void *)0. C++11 defines a new null pointer constant nullptr that can only be converted to any pointer type, pointer-to-member type, or bool type.
🌐
Wikibooks
en.wikibooks.org › wiki › More_C++_Idioms › nullptr
More C++ Idioms/nullptr - Wikibooks, open books for an open world
November 3, 2007 - #include <typeinfo> struct C { void func(); }; template<typename T> void g( T* t ) {} template<typename T> void h( T t ) {} void func (double *) {} void func (int) {} int main(void) { char * ch = nullptr; // ok func (nullptr); // Calls func(double *) func (0); // Calls func(int) void (C::*pmf2)() ...
🌐
Rip Tutorial
riptutorial.com › nullptr
C++ Tutorial => nullptr
void f(int* p); template <class T> void g(T* p); void h(std::nullptr_t p); int main() { f(nullptr); // ok g(nullptr); // error h(nullptr); // ok }
🌐
Tutorialspoint
tutorialspoint.com › cplusplus › cpp_nullptr.htm
nullptr in C++
In this example, we are calling display() function by passing value as '0'. It calls the function that has an int parameter rather than calling the function that has a pointer in its parameter.
🌐
DZone
dzone.com › articles › what-exactly-nullptr-is-in-c
What Exactly Nullptr Is in C++? - DZone
April 16, 2020 - NULL is 0 (zero) i.e. integer constant zero with C-style typecast to void*, while nullptr is prvalue of type nullptr_t, which is an integer literal that evaluates to zero.
🌐
Reddit
reddit.com › r/cpp_questions › what is the difference between null and nullptr when using them for something like a binary search tree? are those interchangeable?
r/cpp_questions on Reddit: What is the difference between NULL and nullptr when using them for something like a Binary Search Tree? Are those interchangeable?
June 29, 2021 -

As far as I know, NULL is just another name for 0 (edit 3: I used the NULL from <iostream>)for which nullptr is a pointer pointing 0. Some references I have used NULL, but others have nullptr. They are writing their trees in different ways so I am not sure if they are interchangeable, but I saw someone on an overflow post saying that versions of C++ do consider NULL == nullptr. But my past experiences are saying that int *some operator here* pointer is a mess. Can anyone clear this up for me? Thank you!

edit:sample for NULL:TreeNode *leftPtr = NULL;TreeNode *rightPtr = NULL;

sample for nullptr:TreeNode *leftPtr = nullptr;TreeNode *rightPtr = nullptr;// idk if these work, or if they only work if:// for example rootPtr->rightPtr = nullptr

// edit 2: running this, it prints "1" so it must be true,

#include <iostream>

using namespace std;

int main() {
    cout << (NULL == nullptr) << endl;
    return 0;
}

Top answer
1 of 5
9
As far as I know, NULL is just another name for 0 It is not. If you look here , you will see that NULL is defined in all of: It is also defined in all of: You'll also find NULL defined in a lot of 3rd party libraries. And in all instances it is IMPLEMENTATION DEFINED. It's not at all unreasonable to find #define NULL ((void*)(0)) or #define NULL ((char*)(0)) or any other such nonsense, both of which are both INCORRECT AND NOT THE SAME THING. Problems arose when, for a misplaced sense of brevity, K&R decided to reuse integer zero in a different context, where null doesn't mean the same thing. I guess this saved them some punch card space, but it introduced a lot of misunderstanding and conflation of what both null and pointers are, and we've been dealing with this confusion as a source of bugs and exploits ever since. Then Bjarne admits he made the same mistake when declaring pure virtual methods as equal to zero. There are many scenarios that can arise where you mean an integer and get a null pointer, or want a null pointer and get an integer. nullptr is type safe. Never use NULL, there is no scenario where it is necessary or preferred, not even for backward compatibility or when interfacing with a C library.
2 of 5
7
nullptr is the null pointer literal introduced with c++11. It has implicit conversion to any pointer type. It's a keyword in c++. NULL is an implementation-defined null pointer constant, defined in a number of different headers (and I guess pulled in indirectly through your implementation of the iostream header).
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › understanding-nullptr-c
Understanding nullptr in C++ - GeeksforGeeks
Examples · Quizzes · Projects · Cheatsheet · OOP · Exception Handling · STL · DSA C++ Last Updated : 9 Jun, 2026 · In C++, NULL was traditionally used to represent null pointers. However, its use can lead to ambiguity in function overloading and unintended type conversions. To overcome these issues, C++11 introduced nullptr, a type-safe keyword that clearly represents a null pointer and eliminates ambiguity in pointer operations.
Published: June 9, 2026
🌐
cppreference.com
en.cppreference.com › c › language › nullptr
Predefined null pointer constant (since C23) - cppreference.com
#include <stddef.h> #include <stdio.h> void g(int*) { puts("Function g called"); } #define DETECT_NULL_POINTER_CONSTANT(e) \ _Generic(e, \ void* : puts("void*"), \ nullptr_t : puts("nullptr_t"), \ default : puts("integer") \ ) int main() { g(nullptr); // OK g(NULL); // OK g(0); // OK auto cloned_nullptr = nullptr; g(cloned_nullptr); // OK [[maybe_unused]] auto cloned_NULL = NULL; // g(cloned_NULL); // implementation-defined: maybe OK [[maybe_unused]] auto cloned_zero = 0; // g(cloned_zero); // Error DETECT_NULL_POINTER_CONSTANT(((void*)0)); DETECT_NULL_POINTER_CONSTANT(0); DETECT_NULL_POINTER_CONSTANT(nullptr); DETECT_NULL_POINTER_CONSTANT(NULL); // implementation-defined }
🌐
AlgoMaster
algomaster.io › home › c++ › nullptr (c++11)
nullptr | C++ | AlgoMaster.io
June 6, 2026 - nullptr is the C++11 keyword for "this pointer points to nothing". It replaces the older habits of writing 0 or the macro NULL, which both predate C++ and cause real bugs once function overloading enters the picture.
🌐
Runebook.dev
runebook.dev › en › docs › cpp › types › null
cpp - C++ NULL vs. nullptr: A Guide to Modern Null Pointers
Here’s the previous example, but with nullptr · void print(int x) { std::cout << "Integer version: " << x << std::endl; } void print(char* s) { std::cout << "Pointer version: " << (s ?