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.
Technical Properties
Type Classificationstd::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*.
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 tofalse. Implicit conversion (copy-initialization) toboolis explicitly ill-formed to prevent unintended type coercions. - Integers: Ill-formed.
std::nullptr_tcannot be implicitly converted to integral types (e.g.,int,size_t). This prevents accidental pointer-to-integer arithmetic or narrowing conversions.
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.
Equality and Comparison
The C++ standard defines strict rules for comparingstd::nullptr_t operands:
- Equality (
==,!=): Comparing twostd::nullptr_tobjects is well-formed and always evaluates as equal. Comparing astd::nullptr_tobject 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 operatoroperator<=>(std::nullptr_t, std::nullptr_t)which always yieldsstd::strong_ordering::equal. - Relational Comparison (
<,<=,>,>=):- Between two
std::nullptr_tobjects: Prior to C++20, relational comparisons betweenstd::nullptr_toperands were ill-formed becausestd::nullptr_tis 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 < nullptrbecomes(nullptr <=> nullptr) < 0). Strict comparisons (<,>) evaluate tofalse, while non-strict comparisons (<=,>=) evaluate totrue. - Between
std::nullptr_tand 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.
- Between two
Master C++ with Deep Grasping Methodology!Learn More





