Search code examples
c++c++11nullptr

How is nullptr defined?


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?


Solution

  • 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.