Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is nullptr_t not a keyword

Tags:

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?

like image 428
anton_rh Avatar asked Dec 05 '15 15:12

anton_rh


People also ask

What is nullptr_t?

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. Its values are null pointer constants (see NULL), and may be implicitly converted to any pointer and pointer to member type.

Is nullptr same as 0?

nullptr vs NULL NULL is 0(zero) i.e. integer constant zero with C-style typecast to void*, while nullptr is prvalue of type nullptr_t which is integer literal evaluates to zero.

What's the value of nullptr?

The nullptr keyword represents a null pointer value. Use a null pointer value to indicate that an object handle, interior pointer, or native pointer type does not point to an object.

What is the type of nullptr literals in C++?

The keyword nullptr denotes the null pointer literal. It is an unspecified prvalue of type std::nullptr_t. There exist implicit conversions from nullptr to null pointer value of any pointer type and any pointer to member type.


2 Answers

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.

like image 159
101010 Avatar answered Oct 13 '22 12:10

101010


I believe the reason is simple: the standardization committee wants to avoid as much as reasonably possible to introduce new keywords (because, given the billions lines of existing C++ code, it is likely to conflict with some code somewhere).

Since std::nullptr_t is definable, it does not need to be a keyword.

And bool is a keyword for historical reasons. It very probably has been introduced quite early...

C++ is mostly about legacy software, so human and social and economical considerations (and backward compatibility) matters a lot for the standardization committee (often more than technical reasons alone).

like image 37
Basile Starynkevitch Avatar answered Oct 13 '22 13:10

Basile Starynkevitch