liveBook · Manning

Nullptr_t

As explained in Modern C, Third Edition

nullptr_t is the C23 distinguished type of the nullptr value and is distinct from every pointer type.

nullptr_t is the C23 distinguished type of the nullptr value. Unlike an ordinary pointer type, it remains separately identifiable, allowing generic selection to recognize nullptr specifically. section 18.2

Selecting a nullptr branch

A type-generic snprintf wrapper can select a special branch when its argument has type nullptr_t: a call using nullptr becomes effectively `(snprintf)(nullptr, 0, format, a, b, c)`, while a buffer selects the reordered `snprintf_swapped(n, buffer, format, a, b, c)` form. section 18.2

Keep this constraint in mind:

  • Every choice expression in a _Generic selection must be valid, including branches that will not be evaluated. section 18.2
_Generic(\textit{controlling expression},
  \textit{type1}: \textit{expression1},
  ... ,
  \textit{typeN}: \textit{expressionN})

Use the generic-selection form to associate nullptr_t with a specialized expression. section 18.2

How Modern C, Third Edition covers it

How deep it goes
Explains its distinction from pointer types and its role in keeping unselected generic branches valid.
Examples
Uses a type-generic snprintf wrapper with nullptr and buffer arguments.
What you can do after
Apply via _Generic to dispatch nullptr and pointer arguments safely in a macro.
How it connects
Connects nullptr_t to nullptr, _Generic, generic selection, pointer types, and C23.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.