8

As far as I'm aware nullptr is a part of the core language.

Quoting C++11: (18.2/9)

nullptr_t is defined as follows:

namespace std { typedef decltype(nullptr) nullptr_t; }

and is defined in the header <cstddef>.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
embedc
  • 1,485
  • 1
  • 6
  • 20
  • 3
    related/maybe dupe: https://stackoverflow.com/questions/39080709/why-can-i-use-nullptr-without-including-stl – NathanOliver Aug 23 '19 at 14:45
  • 5
    You don't need `nullptr_t` if you are willing to live with `decltype(nullptr)`. `nullptr_t` isn't a core concept, it's a helper. – François Andrieux Aug 23 '19 at 14:49
  • You need `nullptr_t` sometimes: "If two or more overloads accept different pointer types, an overload for `std::nullptr_t` is necessary to accept a null pointer argument." (cppreference for std::nullptr_t). – Adrian Mole Aug 23 '19 at 15:08
  • 2
    @Adrian I'm assuming you meant to reply to my comment. If so, I said you don't need `nullptr_t` **if you are willing to live with `decltype(nullptr)`**. You can use `decltype(nullptr)` instead of `nullptr_t`. `nullptr_t` is just a convenient alias for `decltype(nullptr);`. You *should* use `nullptr_t`, my comment is meant to indicate why `nullptr_t` might not be part of the core language. – François Andrieux Aug 23 '19 at 15:10
  • Also by making `nullptr` part of the language, it is much easier to ensure that it works as it should because you can easily have specific rules. – Phil1970 Aug 24 '19 at 14:42

3 Answers3

7

Because it can. A central aim in the C++ standardization process is to alter the core language as little as possible when adding to the language.

nullptr usurps the use of 0 to mean both a null pointer and, er, zero. Using 0 for both caused problems for obvious reasons, does f(0) call f(int) or f(int*)? So a brand new literal was added to the core language: nullptr. Its type is simply decltype(nullptr) so nullptr_t was added as a short cut:

namespace std {
    using nullptr_t = decltype(nullptr);
}
Paul Evans
  • 27,315
  • 3
  • 37
  • 54
  • Thank you, but the nullptr itself can be a part of the STL... Is nullptr included in the core language to not require developers to include any header files? – embedc Aug 23 '19 at 14:54
  • @embedc Any `std` typedefs like e.g. `std::stize_t`are provided through headers, so what? – πάντα ῥεῖ Aug 23 '19 at 14:59
  • 2
    @embedc `nullptr` is a literal like `5`, `'a'` or `"hello"`. While I can't imagine what it is, I would bet there is a good technical reason why a null pointer literal is better than having a `nullptr`-like object in `std`. Edit : For one thing, being a literal it's a prvalue so you can't take it's address. It would seem strange to me to be able to take `nullptr`'s address. It would be like getting `true`'s address. – François Andrieux Aug 23 '19 at 14:59
  • 1
    No headers are required. It is a built-in keyword / pointer literal. – Ron Aug 23 '19 at 15:03
6

The proposal that introduced nullptr, N2431, indicates in section 1.1 that it was desirable to not force users to include a header in order to use nullptr.

It also remarks, "We do not expect to see much direct use of nullptr_t in real programs". Thus, it was considered preferable to add nullptr_t to the library rather than create a new keyword only to be used for this obscure purpose. In addition, if you don't want to include the header, you can always just write decltype(nullptr) yourself.

Brian Bi
  • 111,498
  • 10
  • 176
  • 312
1

From cppreference.com:

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.

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

You can then solve overloaded function call ambiguity with std::nullptr_t.

For example:

void Foo(int* ptr) {}
void Foo(double* ptr) {}
void Foo(std::nullptr_t ptr) {} // This overload is called if Foo(nullptr) is invoked

Read more about std::nullptr_t here: https://en.cppreference.com/w/cpp/types/nullptr_t

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Adrien Givry
  • 956
  • 7
  • 18