std::nullptr_t(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

🌸 std::nullptr_t(3) β€” C++ Standard Library, bestie edition πŸ’…

okay so this page lives in the C++ Standard Library section of the manual, and it's all about std::nullptr_t. Pull up a chair, we're gonna get into it 🫢


✨ NAME ✨

std::nullptr_t β€” literally std::nullptr_t, that's the whole name, no notes πŸ‘‘


πŸ¦‹ Synopsis πŸ¦‹

First thing's first, where does it live? It's Defined in header <cstddef>:

using nullptr_t = decltype(nullptr);  (since C++11)

So std::nullptr_t is the type of the null pointer literal, nullptr. And here's the iconic part: it's a distinct type β€” it's not itself a pointer type, and it's not a pointer to member type. Not me checking its ID at the door 😭

Its values are null pointer constants (peep NULL), and they may be implicitly converted to any pointer and pointer to member type. Slay, flexible queen πŸ’–

Also, sizeof(std::nullptr_t) is equal to sizeof(void *). Same size, different vibe ✨


πŸ’œ Notes πŸ’œ

  • The C++ standard requires <stddef.h> to place the contents of <cstddef> in the global namespace, and thereby requires nullptr_t to be available in the global namespace when <stddef.h> is included.
  • nullptr_t is not a part of C until C23. (C catching up years later, as per usual πŸ™ˆ)
  • It is unspecified whether the declaration of std::nullptr_t is available in any other standard library header. An implementation may avoid introducing this name even when the standard requires std::nullptr_t to be used β€” by e.g. spelling decltype(nullptr) instead. so sneaky, lowkey 🌸

πŸŽ€ Example πŸŽ€

So here's the situation: if two or more overloads accept different pointer types, an overload for std::nullptr_t is necessary to accept a null pointer argument. that's the whole point, bestie, no cap πŸ”₯

// Run this code

#include <cstddef>
#include <iostream>

void f(int*)
{
   std::cout << "Pointer to integer overload\n";
}

void f(double*)
{
   std::cout << "Pointer to double overload\n";
}

void f(std::nullptr_t)
{
   std::cout << "null pointer overload\n";
}

int main()
{
    int* pi{};
    double* pd{};

    f(pi);
    f(pd);
    f(nullptr); // would be ambiguous without void f(nullptr_t)
    // f(0);    // ambiguous call: all three functions are candidates
    // f(NULL); // ambiguous if NULL is an integral null pointer
               constant
                // (as is the case in most implementations)
}

⭐ Output ⭐

Pointer to integer overload
Pointer to double overload
null pointer overload

ate and left no crumbs πŸ’€ the nullptr overload is the main character here, fr fr πŸ‘‘


🧚 See also 🧚

  • nullptr (C++11) β€” the pointer literal which specifies a null pointer value
  • NULL β€” implementation-defined null pointer constant (macro constant)
  • is_null_pointer (C++14) β€” checks if a type is std::nullptr_t (class template)
  • C documentation for nullptr_t

and that's the whole page, bestie! πŸ’– this gem came from http://cppreference.com, version 2024.06.10, and it was filed under std::nullptr_t(3) β€” go forth and overload responsibly πŸ¦‹βœ¨πŸŽ€

πŸ“¦ where this page comes from