Search code examples
c++c++11nullptr

Why is nullptr a part of the core language, but nullptr_t is a part of STL?


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>.


Solution

  • 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.