Firstly, std::nullptr_t
is a core language type, so why is it in the std
namespace? You don’t see std::char
or std::int
.
Secondly, where is it in the std
namespace? When I right click in Visual Studio to see where it is declared, it says "the symbol nullptr_t is not located in any source file." If std::nullptr_t
is not declared in the std
namespace, why does code containing std::nullptr_t
compile?
EDIT:
This link on microsoft’s website says nullptr is a built in type, and that built in types are not defined in header files.
3
Answers
std::nullptr_t
is a fundamental type(aka built in type) as explained here. It is not even a pointer type though it can be implicitly converted to any pointer type. Moreover, it is defined inside headercstddef
in namepsacestd
as quoted below.This can be seen from lex.nullptr:
From cstddef.syn:
In both libstdc++, libc++, and MSVC’s STL,
std::nullptr_t
is a typedef fordecltype(nullptr)
.So yes, the type is a core language type, but it doesn’t have a name, and the only way to refer to it (without the header) is with
decltype(nullptr)
.As others have stated,
std::nullptr_t
is a special type. It is to be distinguished fromnullptr
itself – which is a value.There are some use cases for
std::nullptr_t
.For example function overloads: