15

If I am talking about nullptr, is the correct terminology the nullptr (there is only one, and all mentions are references to the same thing), or a nullptr (there are several identical things).

That is, should function documentation say

If ptr is the nullptr, the function uses an internal buffer...

or

If ptr is a nullptr, the function uses an internal buffer...

(Pedantically, this is not an opinion based question, because it can be answered by reference to what exactly nullptr is)

Raedwald
  • 46,613
  • 43
  • 151
  • 237
  • 8
    https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md seems to *mostly* use no article at all, which I find consistent with saying "x is 5" or "x is true". However, after reading a bit more, I found a few occurrences of "the nullptr". – mkrieger1 May 18 '18 at 12:31
  • I use "the nullptr" only when that phrasing gives insight to the problem domain. For instance, if I am making functions that return sets of things, then I might return "the nullptr" for the empty set. Though usually I'd actually return an empty iterable of some kind to save a branch in usercode. – hoodaticus May 18 '18 at 12:42
  • For a pointer, I'd say null. If referring to nullptr in particular as a keyword that does magical things regarding nulliness, I'd use nullptr. If the meta-topic is about the nullptr itself, then I'd use the nullptr (in the same way that I'd be talking about the true or the false, which is at a meta-level, rather than about true and its truthiness, or false and its property of being falsy). – Eljay May 18 '18 at 13:05
  • 2
    I like the use of the `language-lawyer` tag here. – pipe May 18 '18 at 13:22
  • 1
    I'm trying to see value in the question, but not being successful. This seems more like an editorial problem, since it doesn't alter the semantics of the language. I'd even argue this isn't on-topic. – Passer By May 18 '18 at 16:12
  • @Passer By To provide the correct answer to thus question you must understand what exactly `nullptr` is. So not an arbitrary word choice. Refer to the highly upvotes answers for the subtleties that must be considered. – Raedwald May 18 '18 at 16:34
  • 1
    @Raedwald I don't understand. How then is it different from the linked "What exactly is a nullptr?" – Passer By May 18 '18 at 16:44
  • 1
    Do you ever say `x is the 5` or `x is a 5`? The premise of your question ("how do I write this in documentation?") is partly about English grammar and not technicality of the language. I think you should have been more direct about what you actually want to know - "what is the formal definition of nullptr." – oldmud0 May 18 '18 at 17:12
  • I guess, [this](https://channel9.msdn.com/Shows/Going+Deep/Stephan-T-Lavavej-Everything-you-ever-wanted-to-know-about-nullptr) is all you need. – JohnyL May 18 '18 at 18:52

3 Answers3

24

The standard itself uses three forms interchangeably, but none of them is "is a nullptr" or "is the nullptr". If you use any of these three, you're in good company.

is null

int_type underflow();

7 Remarks: The public members of basic_streambuf call this virtual function only if gptr() is null or gptr() >= egptr()

is a null pointer

int sync();

38 Effects: [...] After constructing a sentry object, if rdbuf() is a null pointer, returns -1. [...]

is nullptr

void lock();

3 Throws: [...] system_error with an error condition of operation_not_permitted if pm is nullptr. [...]

Community
  • 1
  • 1
  • 2
    FWIW, I'd consider the third case, "if `pm` is `nullptr`", to be an editorial error. `pm` is an (exposition-only) lvalue expression of pointer type, and `nullptr` is a prvalue expression of non-pointer type, so I wouldn't say that technically `pm` *can* "be" `nullptr`. I'd use the first or second phrasing instead. – Quuxplusone May 18 '18 at 18:12
15

nullptr is a keyword which designates a prvalue of type std::nullptr_t. Super-formally, a pointer can therefore not be [a|the] nullptr. It can, however, "be null" (or "have a null value").

In your documentation example, I would simply say

If ptr is null, the function ...

Note that std::nullptr_t (and thus nullptr) is not even a pointer type! It's a special type which can be converted to any pointer to object, pointer to function, or pointer to member. Which I consider as extra justification for not speaking about pointers being nullptr; pointers are just null.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • 3
    And `nullptr` is _the null pointer literal_. – YSC May 18 '18 at 12:57
  • @YSC - and it is *a* null pointer constant. – Peter May 18 '18 at 13:22
  • @Peter mmh ... not sure. It is convertible into a null pointer of any type, but it is not a pointer _per se_. – YSC May 18 '18 at 13:23
  • 1
    @YSC "[ *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* ]" C++17 5.13.7/1 – Angew is no longer proud of SO May 18 '18 at 13:35
  • Ho yes! Thank you @Angew. – YSC May 18 '18 at 13:36
0

One context where it might make sense to say “a nullptr” is that there is a NULL pointer value for every pointer type, and these are not necessarily the same value for all types in every implementation. (Although you have to go back to some obsolete computers from nearly fifty years ago to find exceptions.) Someone might call int* maybe_int_p = nullptr; “a nullptr” and char* maybe_string_p = nullptr; “a nullptr,” even though neither of these is exactly the same as the constant of type nullptr_t. I’d pronounce it the same as “a NULL pointer” anyway.

Even then, you would still say “the nullptr” in a context where it was clear which you meant.

As for actual usage, Google ngram viewer can't find any examples of either. As an earlier answer pointed out, neither term is the official one. So there’s no established usage to follow, neither one sounds off to me, and people won’t have any trouble understanding what you meant. If you’re referring to the keyword nullptr, that’s a singleton, so it feels more logical to call it “the.” But use whichever you prefer.

Davislor
  • 14,674
  • 2
  • 34
  • 49