1

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.

5
  • 2
    std::nullptr_t is a core language type no, it is not Commented Dec 8, 2022 at 5:45
  • 1
    where is it in the std namespace? Defined in header <cstddef> see en.cppreference.com/w/cpp/types/nullptr_t Commented Dec 8, 2022 at 5:45
  • 2
    char and int are keywords. They are reserved and aren't scoped to a namespace.
    – jamesdlin
    Commented Dec 8, 2022 at 5:47
  • No, std::nullptr_t is not even a pointer type. Commented Dec 8, 2022 at 5:48
  • 1
    It's a typedef for an unnamed type. That's all. using nullptr_t = decltype(nullptr); Commented Dec 8, 2022 at 7:29

3 Answers 3

5

std::nullptr_t is a core language type

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 header cstddef in namepsace std as quoted below.

This can be seen from lex.nullptr:

The pointer literal is the keyword nullptr. It is a prvalue of type std​::​nullptr_­t. [ Note: std​::​nullptr_­t is a distinct type that is neither a pointer type nor a pointer-to-member type; rather, a prvalue of this type is a null pointer constant and can be converted to a null pointer value or null member pointer value. See [conv.ptr] and [conv.mem]. — end note  ]

From cstddef.syn:

Header <cstddef> synopsis

namespace std {
 using nullptr_­t = decltype(nullptr);

 //other things here
}
6
  • This link says nullptr_t is a built in type: learn.microsoft.com/en-us/cpp/cpp/…. On that same page it says built in types are not defined in header files. Is std::nullptr_t an exception? I can't find where it is defined in cstddef. Commented Dec 8, 2022 at 5:53
  • @beangod See my updated answer where I've mentioned where exactly it is defined in namespace std. So it answers your edited question. Commented Dec 8, 2022 at 6:00
  • Thank you. For some reason in visual studio, the cstddef document in visual studio does not contain that using statement. Commented Dec 8, 2022 at 6:04
  • @beangod Strange. You can also contact msvc support and point this out so that they can further clarify this as VS implementors can say for sure what is happening here in their product. You're welcome. Commented Dec 8, 2022 at 6:07
  • I have an update! nullptr_t is aliased in stddef.h, not cstddef.h. I guess this is because csteddef used to be called stddef in the C standard library. Commented Dec 8, 2022 at 6:36
3

In both libstdc++, libc++, and MSVC's STL, std::nullptr_t is a typedef for decltype(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).

0

As others have stated, std::nullptr_t is a special type. It is to be distinguished from nullptr itself - which is a value.

There are some use cases for std::nullptr_t.

For example function overloads:

void foo(int*);
void foo(nullptr_t);

int main() {
    int a = 4;
    int* b = &a;
    foo(b); // will call foo(int*)
    foo(nullptr); // will call foo(nullptr_t)
    b = nullptr;
    foo(b); // will call foo(int*)
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.