C++ – Why is nullptr_t Not a Keyword

c++c++11

Here is a declaration of nullptr_t in <cstddef> :

namespace std {
  typedef decltype(nullptr) nullptr_t;
}

According to this, std::nullptr_t is an alias for some unspecified fundamental type of which nullptr is an instance. So the actual type of nullptr doesn't have a name (well, the language does not give it a name, the name was given by standard library).

nullptr itself is a keyword. But standard did not introduce a keyword for type of nullptr. Instead using decltype(nullptr) is offered.

What are reasons for doing this? I found it much confusing. You need to include header and specify std:: for just using a language built-in feature.

Is this to keep the set of C++ keywords as small as possible? Is this specifically for nullptr or committee is going to declare all new types like this, so we would have namespace std { typedef decltype(false) bool; } if such decision was made earlier?

Best Answer

According to the initial proposal of nullptr, N2431 (Emphasis Mine):

We propose a new standard reserved word nullptr. The nullptr keyword designates a constant rvalue of type decltype(nullptr). We also provide the typedef: typedef decltype(nullptr) nullptr_t; nullptr_t is not a reserved word. It is a typedef (as its _t typedef indicates) for decltype(nullptr) defined in <cstddef>. We do not expect to see much direct use of nullptr_t in real programs.

Generally, the committee is reluctant in the addition of new keywords in the language. In my humble opinion this is for a good reason, named backward compatibility.

If you further read the proposal, you'd realize that the main concern is not breaking existing code.

Imagine the effect that would have if the committee now and then introduced a new keyword. All hell would break loose and C++ from a success story would have been a big joke.

Related Question