std::nullptr_t(3)
πΈ 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 requiresnullptr_tto be available in the global namespace when<stddef.h>is included. nullptr_tis not a part of C until C23. (C catching up years later, as per usual π)- It is unspecified whether the declaration of
std::nullptr_tis available in any other standard library header. An implementation may avoid introducing this name even when the standard requiresstd::nullptr_tto be used β by e.g. spellingdecltype(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 valueNULLβ implementation-defined null pointer constant (macro constant)is_null_pointer(C++14) β checks if a type isstd::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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms