Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Keywords and Statements

The nullptr literal

Reference for C++ nullptr: the type-safe null pointer literal of type std::nullptr_t, why it replaces NULL and 0, overload resolution, comparisons and templates.

nullptr (C++11) is the null pointer literal. It has its own type, std::nullptr_t, and converts implicitly to any pointer type and to any pointer-to-member type, but not to integers (other than bool in direct contexts). That makes it safer than the old alternatives 0 and NULL, which are integers and can pick the wrong function overload.

A null pointer points to no object. Comparing against it is always safe; dereferencing it is undefined behavior.

C++
int* p = nullptr;
if (p == nullptr) { /* nothing to point at */ }
if (!p) { /* same test */ }

Try it

C++ nullptr versus 0 in overload resolution

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Safe checks, smart pointers and templates

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
nullptr                       // prvalue of type std::nullptr_t
std::nullptr_t                // in <cstddef>
T* p = nullptr;               // any object, function or member pointer
Property Description
Type std::nullptr_t; sizeof equals sizeof(void*)
Converts to Every pointer type, pointer-to-member type, and bool in direct-initialization or conditions
Does not convert to Integer types
Comparisons == and != with any pointer; nullptr == nullptr is true

Usage notes

  • Use nullptr everywhere C used NULL or 0 for pointers. In C++ NULL is an implementation-defined null pointer constant that is often just 0 or 0L, which is why f(NULL) can call f(int).
  • Dereferencing a null pointer, or calling a non-static member function through it is undefined behavior; delete p and free(p) with a null p are fine and do nothing. Always check a pointer you did not create.
  • Prefer references when null is not a valid state; prefer std::optional<T> for "maybe a value"; use smart pointers (which compare against nullptr) for owners.
  • Pointers inside a freshly created object are indeterminate unless you initialize them: write T* p = nullptr; or use a default member initializer.
  • A std::nullptr_t parameter lets a function (or smart-pointer constructor) accept the literal explicitly, as std::unique_ptr(nullptr_t) does.
  • Null pointer values are not guaranteed to have all bits zero on every architecture, so do not use memset to create null pointers.
  • In templates, nullptr deduces as std::nullptr_t, not as a pointer type, so a deduced T will not be a pointer. Overload on std::nullptr_t or pass an explicitly typed pointer when that matters.
  • In C23, nullptr is also a keyword with type nullptr_t.