Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

std::nullptr_t is the fundamental data type of the null pointer literal nullptr. Defined in the <cstddef> header, it serves as a distinct, standalone type that represents a null pointer constant, resolving the type ambiguity historically associated with the NULL macro or the integer literal 0.
#include <cstddef>

// The standard library conceptually defines it as:
// using nullptr_t = decltype(nullptr);

Technical Properties

Type Classification std::nullptr_t is classified as a scalar and fundamental type. However, it is strictly neither a pointer type nor a pointer-to-member type. It is a unique type specifically designed to bridge the gap between integer literals and pointer semantics. Memory, Size, and Alignment Instances of std::nullptr_t occupy memory. The C++ standard guarantees that its size is identical to that of a standard void*. However, the standard does not guarantee that the alignment requirement of std::nullptr_t is identical to that of void*.
#include <cstddef>

static_assert(sizeof(std::nullptr_t) == sizeof(void*));
// Note: alignof(std::nullptr_t) == alignof(void*) is NOT guaranteed
Conversion Rules The compiler enforces strict conversion rules for std::nullptr_t to ensure type safety:
  • Pointers: Implicitly convertible to any raw pointer type (T*) or pointer-to-member type (T U::*). The result is a null pointer value of the target type.
  • Booleans: Contextually convertible to bool (e.g., within conditional statements) and permitted via direct-initialization. The conversion always evaluates to false. Implicit conversion (copy-initialization) to bool is explicitly ill-formed to prevent unintended type coercions.
  • Integers: Ill-formed. std::nullptr_t cannot be implicitly converted to integral types (e.g., int, size_t). This prevents accidental pointer-to-integer arithmetic or narrowing conversions.
struct MyClass {};
std::nullptr_t null_val = nullptr;

int* ptr = null_val;                 // Valid: Implicit conversion to T*
void (MyClass::*m_ptr)() = null_val; // Valid: Implicit conversion to pointer-to-member

bool b1{null_val};                   // Valid: Direct-initialization (evaluates to false)
if (null_val) {}                     // Valid: Contextual conversion to bool

// bool b2 = null_val;               // Error: Copy-initialization is ill-formed
// int x = null_val;                 // Error: No conversion to integral types

Type Traits Integration

The standard library provides specific type traits in the <type_traits> header to identify and evaluate std::nullptr_t at compile time. Because it is not a standard pointer, it fails std::is_pointer checks but passes its own dedicated trait.
#include <type_traits>
#include <cstddef>

static_assert(std::is_null_pointer_v<std::nullptr_t>);
static_assert(std::is_scalar_v<std::nullptr_t>);
static_assert(std::is_fundamental_v<std::nullptr_t>);

// Note: It is NOT considered a pointer or an integer by the type system
static_assert(!std::is_pointer_v<std::nullptr_t>);
static_assert(!std::is_integral_v<std::nullptr_t>);

Equality and Comparison

The C++ standard defines strict rules for comparing std::nullptr_t operands:
  • Equality (==, !=): Comparing two std::nullptr_t objects is well-formed and always evaluates as equal. Comparing a std::nullptr_t object with any valid pointer type is well-formed and evaluates whether the pointer is null.
  • Three-Way Comparison (<=>) (C++20): The standard provides a built-in operator operator<=>(std::nullptr_t, std::nullptr_t) which always yields std::strong_ordering::equal.
  • Relational Comparison (<, <=, >, >=):
    • Between two std::nullptr_t objects: Prior to C++20, relational comparisons between std::nullptr_t operands were ill-formed because std::nullptr_t is not an arithmetic, enumeration, or pointer type. C++20 made these comparisons well-formed for the first time by introducing the built-in <=> operator and resolving the relational operators via rewritten candidates (e.g., nullptr < nullptr becomes (nullptr <=> nullptr) < 0). Strict comparisons (<, >) evaluate to false, while non-strict comparisons (<=, >=) evaluate to true.
    • Between std::nullptr_t and a pointer type: Ill-formed. The standard explicitly prohibits ordering comparisons between pointers and null pointer constants, as null pointers do not possess a strict weak ordering relative to valid memory addresses.
Master C++ with Deep Grasping Methodology!Learn More