4

In Effective Modern C++, "Iterm 8: Prefer nullptr to 0 and NULL.", Page 59, it says:

nullptr's actual type is std::nullptr_t, and, in a wonderfully circular definition, std::nullptr_t is defined to be the type of nullptr.

A wonderfully circular definition?

How that comes?

8
  • 2
    It's a trick in the compiler itself
    – JVApen
    Commented Aug 18, 2018 at 20:32
  • Actural? Is that a misquote? Commented Aug 18, 2018 at 20:40
  • You can find some explanation here Commented Aug 18, 2018 at 20:40
  • 5
    The quote is misleading. Although the type of nullptr is std::nullptr_t, the standard does not use it to define nullptr. So there is no circular definition. The behavior of nullptr and std::nullptr_t are defined by various statements scattered in the standard. Commented Aug 18, 2018 at 20:44
  • 3
    @Eljay: That would make a non-null std::nullptr_t *.
    – Ben Voigt
    Commented Aug 18, 2018 at 20:58

1 Answer 1

2

In brief, nullptr is a value that can be assigned to a pointer to any type, and it is false in boolean context (unlike most pointers that are results of new/malloc, or referencing a valid object), and despite itself being a valid pointer, dereferencing it results in UB, as does in/decrementing it, and it is a sole value of a singleton type nullptr_t.

Something like this.

3
  • that reads like a random pointer not holding a value returned by malloc/new or something returned by the address-of operator would magically would evaluate to false when converted to bool.
    – Swordfish
    Commented Aug 18, 2018 at 21:29
  • 1
    @M.M: as well as new (e.g. in new (std::nothrow)) Commented Aug 18, 2018 at 22:25
  • 1
    "and despite itself being a valid pointer" -- nullptr is not a pointer and cannot be dereferenced. It can be implicitly converted to a pointer type, and after that dereferencing it results in UB, but nullptr itself, no.
    – user743382
    Commented Aug 18, 2018 at 23:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.