CKTIS / C++
nullptr — the null pointer literal
nullptr is a null pointer constant with the distinct type std::nullptr_t. Use it to initialize and compare pointers instead of the numeric literal 0, especially when calling overloaded functions.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
nullptrnullptr is a keyword and a literal of type std::nullptr_t. This distinct type is neither a pointer type nor a pointer-to-member type. The literal can implicitly convert to a null object pointer, function pointer, or member pointer value. Using nullptr itself requires no header.
Writing int* p = nullptr; initializes a null pointer, and p == nullptr tests that state. Do not read or write an object through a null pointer: such use has undefined behavior. A null pointer does not point to an object whose value is zero.
The integer literal 0 can also serve as a null pointer constant, but it still has type int. In the example, select(0) selects the overload taking int, whereas select(nullptr) selects the version taking int*. nullptr does not implicitly convert to int. It does not resolve every overload, however: with only select(int*) and select(double*), calling select(nullptr) would be ambiguous.
This entry targets C++23. The supplied source is an evolving working draft, not a frozen C++23 publication. A compiler flag selects the language mode; it does not attest production runtime compatibility.
Examples
#include <iostream>
const char* select(int) { return "int"; }
const char* select(int*) { return "int*"; }
int main() {
int* p = nullptr;
std::cout << select(0) << '\n';
std::cout << select(nullptr) << '\n';
std::cout << (p == nullptr ? "null" : "non-null") << '\n';
}
Result
int int* null
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.