-1

As far as I'm aware nullptr is an instance of some class, something like this:

const
class {
public:
    template<class T> // convertible to any type
    operator T*() const // of null non-member
    { return 0; } // pointer...
    template<class C, class T> // or any type of null
    operator T C::*() const // member pointer...
    { return 0; }
private:
    void operator&() const; // whose address can't be taken
} nullptr = {};

I suppose that

true 
false 

are also some variables. So is the fact that thay are prvalue-s a dirty hack violating the language's type system?

If nullptr is just a keyword, then how can type be derived from it?

typedef decltype(nullptr) nullptr_t;
Igor R.
  • 14,716
  • 2
  • 49
  • 83
Alexey
  • 710
  • 3
  • 7
  • 19
  • 5
    No, `nullptr` isn't an instance of a class. Neither is `true` or `false`. – Sam Varshavchik Sep 01 '19 at 13:12
  • What is it, then? – Alexey Sep 01 '19 at 13:13
  • 1
    Those are all three [*keywords*](https://en.cppreference.com/w/cpp/keyword). Which means the compiler recognizes them specifically as any other literal values. – Some programmer dude Sep 01 '19 at 13:14
  • 4
    `true`, `false`, and `nullptr` literals aren't really different from other literals such as `42` or `'a'`. If `42` is a prvalue, why shouldn't `true` be a prvalue? – HolyBlackCat Sep 01 '19 at 13:14
  • 1
    They are values -- of either a `boolean` type, or a `nullptr_t` type. – Sam Varshavchik Sep 01 '19 at 13:14
  • `nullptr` is a prvalue of type `std::nullptr_t`, which is defined as `typedef decltype(nullptr) nullptr_t;`. See also: [std::nullptr_t](https://en.cppreference.com/w/cpp/types/nullptr_t) & [nullptr](https://en.cppreference.com/w/cpp/language/nullptr). – Jesper Juhl Sep 01 '19 at 13:15
  • If nullptr is just a keyword, then how can type be derived from it (see updated question)? – Alexey Sep 01 '19 at 13:19
  • What is it a shocker that a type can be derived from a prvalue? It's no different than `using size_t = decltype(sizeof(0))` – StoryTeller - Unslander Monica Sep 01 '19 at 13:21
  • @StoryTeller sizeof(0) has some type, but just a brand new keyword hasn't. What is type the keyword if? decltype(if) – Alexey Sep 01 '19 at 13:22
  • Yes, and the standard clearly state `nullptr` has a special type too, just like `0` has a type. – StoryTeller - Unslander Monica Sep 01 '19 at 13:26
  • @StoryTeller Why is yet another type for nullptr (nullptr_t) introduced then? The bool isn't introduced as typedef decltype(false) bool; right? – Alexey Sep 01 '19 at 13:29
  • Why? Because Bjarne baked a bool type into the language long before standardization. It's also used far more often than the type of `nullptr`, so it's convenient to have it without needing a library header. `std::size_t` and `std::nullptr_t` don't get the same honor. – StoryTeller - Unslander Monica Sep 01 '19 at 13:40
  • So compiler just treats the keyword nullptr in a special way - that is the implementation of nullptr? – Alexey Sep 01 '19 at 13:45
  • @Alexey "Why is yet another type for nullptr (nullptr_t)" - because it is useful to be able to overload functions on `nullptr_t`, to distinguish from `int` (like `0`). – Jesper Juhl Sep 01 '19 at 13:54

1 Answers1

1

Take a look at the Standard:

8.1.1 Literals [expr.prim.literal] A literal is a primary expression. Its type depends on its form (5.13). A string literal is an lvalue; all other literals are prvalues.

<...>

5.13.6 Boolean literals [lex.bool] boolean-literal: false true

The Boolean literals are the keywords false and true. Such literals are prvalues and have type bool.

5.13.7 Pointer literals [lex.nullptr] pointer-literal: nullptr

The pointer literal is the keyword nullptr. It is a prvalue of type std::nullptr_t. [ Note: std::nullptr_t is a distinct type that is neither a pointer type nor a pointer to member type; rather, a prvalue of this type is a null pointer constant and can be converted to a null pointer value or null member pointer value. See 7.11 and 7.12. — end note ]

So, as you can see, both nullptr and false/true obey the common rule. The only exception is a string literal (because it is essentially an array).

Community
  • 1
  • 1
Igor R.
  • 14,716
  • 2
  • 49
  • 83