0

I was reading this question :

In c++11, does dynamic_cast return nullptr or 0?

I learned that the result of a dynamic_cast can be the null value for that particular type:

Consider a variable char * ptr. It's type is (unsurprisingly) char *. But the type of nullptr is the special type nullptr_t. So when we write something like ptr = nullptr, some technical things must happen

nullptr must be implicitly converted to char *. The result of this conversion is set as the new value of ptr. The null value for char * is the result of converting nullptr to char *. Conceptually, it's still nullptr, but with a different type (char *). This null value is distinct from the null value of int * or string * or any other pointer type. We tend to think of these null values as just nullptr (or 0), but each one is really a distinct value from a different type. (By the way, the same conversion happens for comparison using ==).

So what are the different null pointer types ? Does it really matter ? Is it sufficient in any check (where we want to determine if something is null) to simply check if == to std::nullptr ?

Community
  • 1
  • 1
Rahul Iyer
  • 19,924
  • 21
  • 96
  • 190
  • 1
    `So what are the different null pointer types ?` doesn't make much sense because (as your quote tells you) it's any pointer type. All pointer types have null values. – deviantfan Jun 20 '16 at 03:16
  • `simply check if == to std::nullptr` Unless I missed something, types in C++ are not directly comparable with `==`. And a `char*` or `int*` is never the same as nullptr_t – deviantfan Jun 20 '16 at 03:17
  • 1
    `nullptr` is a keyword, so it's not in the `std` namespace. Only its type, `std::nullptr_t` is in a namespace. – Johan Boulé Jun 20 '16 at 03:19

3 Answers3

4

There aren't different nullptr types. nullptr is a value of type std::nullptr_t. There are different pointer types, and any pointer type can have a null value, which when compared to nullptr, results in true. And the number of pointer types is infinite. Since you can form a pointer to any type, and the result is itself a type which you can form a pointer to. (i.e. you can have int*************************)

Is it sufficient in any check (where we want to determine if something is null) to simply check if == to std::nullptr ?

Not std::nullptr, just nullptr. But otherwise, yes.

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
  • So when we compare with nullptr, C++ checks if the pointer is a null pointer, but its not actually comparing it to a type ? – Rahul Iyer Jun 20 '16 at 03:38
  • @John: Yes to the first, and I'm not quite sure what you mean by the second. – Benjamin Lindley Jun 20 '16 at 03:41
  • Well, if there are an infinite number of types, and we are doing an == comparison with nullptr, but at the same time nullptr is a keyword, and not a type, then we are not actually comparing with a type - we are checking if the object is equivalent conceptually to null. Is that correct ? – Rahul Iyer Jun 20 '16 at 03:43
  • 3
    @John: Well, yeah. I suppose. But other than in template metaprogramming (which is executed at compile time), I don't think we are ever comparing with types. Runtime comparisons are always on values, not on types. – Benjamin Lindley Jun 20 '16 at 03:48
2

So what are the different null pointer types?

There is only one null pointer type, nullptr_t.

Does it really matter?

Since there is only one type of null pointer, this question is moot.

Is it sufficient in any check (where we want to determine if something is null) to simply check if == to std::nullptr ?

There is nothing like std::nullptr. nullptr is a keyword. It is a prvalue of type std::nullptr_t.

Yes, any pointer can be compared with nullptr.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
2

There is a type std::nullptr_t. It is not a pointer type. Despite this fact, we call an instance of this type (such as nullptr) a "null pointer constant".

For every type T, T* is a distinct pointer type, and there is a null pointer of this type.

There are suitable conversions from nullptr_t to any pointer type that allow us to use nullptr whenever we want a null pointer constant. e.g.

int *ptr = nullptr; // initialize to be the null pointer of type int*