Namespaces
Variants
Views
Actions

std::nullptr_t

From cppreference.com
< cpp‎ | types
 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)

Elementary string conversions
(C++17)
(C++17)
Stacktrace
 
Type support
Basic types
Fundamental types
Fixed width integer types (C++11)
nullptr_t
(C++11)
Numeric limits
C numeric limits interface
Runtime type information
Type traits
Type categories
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Type properties
(C++11)
(C++11)
(C++14)
(C++11)
(C++11)(until C++20)
(C++11)(deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Constant evaluation context
Supported operations
Relationships and property queries
Type modifications
(C++11)(C++11)(C++11)
Type transformations
(C++11)
(C++11)
(C++17)
(C++11)(until C++20)(C++17)
 
Defined in header <cstddef>
typedef decltype(nullptr) nullptr_t;
(since C++11)

std::nullptr_t is the type of the null pointer literal, nullptr. It is a distinct type that is not itself a pointer type or a pointer to member type. Its values are null pointer constant (see NULL), and may be implicitly converted to any pointer and pointer to member type.

sizeof(std::nullptr_t) is equal to sizeof(void *).

[edit] Notes

nullptr_t is available in the global namespace when <stddef.h> is included, even if it is not a part of C.

[edit] Example

If two or more overloads accept different pointer types, an overload for std::nullptr_t is necessary to accept a null pointer argument.

#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

[edit] See also

nullptr(C++11) the pointer literal which specifies a null pointer value[edit]
implementation-defined null pointer constant
(macro constant) [edit]
checks if a type is std::nullptr_t
(class template) [edit]