> ## 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.

# C++ std::nullptr_t

`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`.

```cpp theme={"dark"}
#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*`.

```cpp theme={"dark"}
#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.

```cpp theme={"dark"}
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.

```cpp theme={"dark"}
#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.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C++ Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
