nullptr is the C++ keyword for the null pointer literal. It was introduced in C++11, has the type std::nullptr_t, and converts to any pointer type and to bool, but not to an integer. Use it instead of NULL or 0 so the compiler can distinguish a pointer from a number.
The practical benefit appears in overload resolution, template deduction and generic code, where the old spellings choose the wrong function or fail to compile.
nullptr isnullptr replaced NULL
nullptr in everyday code
NULL to nullptrnullptr isnullptr is a prvalue of type std::nullptr_t, declared in <cstddef>. You never need to include a header to write the literal itself. The type has these properties:
std::nullptr_t.bool (as false) in contexts such as if (ptr == nullptr) or when initialising a bool directly.int or long.sizeof(std::nullptr_t) equals sizeof(void*).nullptr replaced NULLNULL is a macro inherited from C. In C++ it expands to an integer constant, such as 0 or 0L, or to a compiler-specific null constant. Because it is an integer in the type system, it behaves like a number whenever a function takes both integers and pointers.
With nullptr, the call always means "pointer". NULL may select display(int) or be rejected as ambiguous, which is almost never what the author intended.
A literal 0 or NULL passed through a template is deduced as int and no longer converts to a pointer. nullptr is deduced as std::nullptr_t, which still converts to pointers at the final call:
Because nullptr does not convert to integers, mistakes such as int x = nullptr; are rejected at compile time.
nullptr in everyday codestd::unique_ptr, std::shared_ptr and std::function all accept nullptr for construction, assignment and comparison:
You can overload on std::nullptr_t to handle the literal separately:
NULL to nullptrNULL and pointer-context 0 with nullptr. A search with a regular expression finds most cases.NULL in headers shared with C code. A C compiler only understands NULL, unless it supports C23.clang-tidy has the check modernize-use-nullptr, and GCC and Clang warn with -Wzero-as-null-pointer-constant.Run the unit tests afterwards, because correcting an overload that was wrongly selected before may change behavior.
nullptr in C++?std::nullptr_t, defined in <cstddef>. It is a distinct fundamental type with exactly one value.
nullptr the same as NULL?No. NULL is a macro for an integer constant, so it participates in integer overloads. nullptr is a keyword with its own type and is the right choice in C++11 and later.
nullptr to an integer?No. The conversion to integer types is not allowed, so int x = nullptr; fails to compile. You can only convert it to bool in boolean contexts.
nullptr work in C?C23 added a nullptr keyword with similar meaning. Earlier C standards use NULL.
nullptr is the type-safe null pointer literal of C++11 and later. It removes overload ambiguity, works correctly through templates and smart pointers, and turns accidental integer use into compile errors. Prefer it everywhere in C++ code and retire NULL and pointer 0.