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?
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.
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.
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.
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.
According to the initial proposal of nullptr
, N2431 (Emphasis Mine):
We propose a new standard reserved word
nullptr
. Thenullptr
keyword designates a constant rvalue of typedecltype(nullptr)
. We also provide thetypedef
:typedef decltype(nullptr) nullptr_t;
nullptr_t
is not a reserved word. It is atypedef
(as its_t
typedef
indicates) fordecltype(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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With