36

It seems that nullptr is declared in the default global namespace. Wouldn't it make sense for it to be in the std namespace?

1
  • 16
    Considering it is supposed to be used instead of null, reducing the required typing can only encourage its use.
    – Dave
    Commented Feb 2, 2014 at 11:41

2 Answers 2

76

nullptr is a C++11 keyword (no different to if, public, true, void, etc.), so namespaces don't apply.

2
  • 45
    More fitting comparision: true/false.
    – Xeo
    Commented Feb 2, 2014 at 11:58
  • 1
    I think he said it just right. The true/false analogy isn't helpful for someone coming from a C background, as true/false in C are just macros defined in stdbool.h which do something like #define true 1 and #define false 0. So, are you saying nullptr is a macro? Is it an integer, like C's true/false? Is it just a zero (0)?--that would kind of defeat the whole purpose of nullptr, now, so that analogy to C++'s true/false really really muddles things up. Commented Aug 20, 2020 at 23:38
29

nullptr is a pointer literal the same way as for example true is a boolean literal. This literal has type std::nullptr_t that is as you see this type is defined in name space std:: The pointer literal is described in section 2.14.7 Pointer literals of the C++ Standard.

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