Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the type of nullptr?

The Standard states, that nullptr is a pointer literal of type std::nullptr_t (2.14.7). And 18.2p9 defines nullptr_t by

namespace std {
  typedef decltype(nullptr) nullptr_t;
}

By 7.1.6.2p4 decltype(nullptr) is the type of the expression nullptr, which is by definition std::nullptr_t (since the expression nullptr is a prvalue). Substituting that into the definition of nullptr_t results in

typedef nullptr_t nullptr_t

On the other hand a typedef specifier does not introduce a new type, it's just a name for another existing type. So, what is exactly nullptr_t? I'm not able to comprehend these definitions.

like image 628
MWid Avatar asked Jun 12 '13 15:06

MWid


People also ask

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.

What is nullptr equal to?

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.

Is nullptr == null?

nullptr is a keyword that can be used at all places where NULL is expected. Like NULL, nullptr is implicitly convertible and comparable to any pointer type. Unlike NULL, it is not implicitly convertible or comparable to integral types.

What nullptr means?

The nullptr keyword specifies a null pointer constant of type std::nullptr_t , which is convertible to any raw pointer type. Although you can use the keyword nullptr without including any headers, if your code uses the type std::nullptr_t , then you must define it by including the header <cstddef> .


2 Answers

Internally there is an entity that is the null pointer constant type. It is one of the fundamental types.

The keyword, literal and expression nullptr has this type. decltype(nullptr) refers to this type.

However the name std::nullptr_t is not a keyword (not even a context-sensitive one), and so the name does not exist until declared. If you refer to the name std::nullptr_t without declaring it, it is an error, as for any undeclared name.

So although the type exists at the start of translation like any fundamental type, the name does not exist.

In fact there are other fundamental types that do not have a "single spelling", such as short int. A short int can be refered to as short, short int, signed short int, signed short, or any permutation thereof.

It is also not dissimilar to the relationship between the typeid operator (keyword), and the type of the typeid(...) expression, std::typeinfo. typeinfo is also not a keyword and the name does not exist before being declared.

Basically, you are conflating an entity (the null pointer constant type) with a name (std::nullptr_t)

If you ask why didn't the language designers specify nullptr_t and typeinfo as keywords, I would speculate that they are not common enough to risk a name collision with a user-defined name with the same spelling. Recall that such a collision would occur in any and all scopes.

like image 108
Andrew Tomazos Avatar answered Sep 24 '22 15:09

Andrew Tomazos


It is implementation-specific. What is important is that (p. 18.2/9 of the C++11 Standard):

[...] The type for which nullptr_t is a synonym has the characteristics described in 3.9.1 and 4.10. [...]

As long as it behaves like the Standard specifies in those two paragraphs, it can be anything.

I believe the logical fallacy in your argument is that this:

By 7.1.6.2p4 decltype(nullptr) is the type of the expression nullptr, which is by definition std::nullptr_t (since the expression nullptr is a prvalue)

Does not mean that nullptr_t is not a type alias. For instance, if I define:

typedef decltype(42) foo;

I can say that the type of the expression:

42

Is foo. Yet, foo is just an alias for another type (int).

like image 29
Andy Prowl Avatar answered Sep 25 '22 15:09

Andy Prowl