2

The names of the smart pointer types std::unique_ptr and std::shared_ptr contain an underscore (_), whereas the keyword nullptr doesn't. So I'm curious: What, if any, is the rationale for using or not using an underscore in the keyword nullptr?

Note: I know nullptr is a keyword serving as null pointer constant, of type nullptr_t, whereas std::unique_ptr is a type. So maybe the rationale is related to keywords not having underscores whereas types often do. Seems a little thin, but maybe. Or perhaps the rationale is related to use of null_ptr versus nullptr in heritage code?

Edit: nullptr became a keyword as of C++11.

Edit (from comment by R2-Dequeue): From Table 3 in 2.11 in the standard, C++14 keywords with underscores:

  • const_cast, dynamic_cast, reinterpret_cast, static_cast
  • static_assert
  • char16_t, char32_t, wchar_t
  • thread_local

And for completeness, also the alternative representations from Table 4:

  • and_eq, or_eq, xor_eq, not_eq
chr
  • 121
  • 8
  • `nullptr` isn't in the standard – Ryan Aug 03 '15 at 23:45
  • I don't believe it is. Can you check? – Ryan Aug 03 '15 at 23:53
  • 2
    I'm going to echo the "huh". – Nir Friedman Aug 04 '15 at 00:03
  • 8
    We ran out of underscores. – Howard Hinnant Aug 04 '15 at 00:05
  • 3
    @self `nullptr` was added to the C++11 standard. It's now standard C++. – templatetypedef Aug 04 '15 at 00:05
  • @self ...for a definition of "now" that extends to four years ago. – Quentin Aug 04 '15 at 14:49
  • @chr `thread_local` is a keyword with an underscore. AFAIK it's the only one though. – Quentin Aug 04 '15 at 14:56
  • @Quentin Just for posterity (and I only just now thought to look), from Table 3 in 2.11 in the standard, the following are all the keywords with underscores in C++14: `reinterpret_cast, dynamic_cast, static_assert, static_cast, char16_t, char32_t, wchar_t, thread_local, const_cast`, though a lot of those take arguments when used and aren't just specifiers. I'm curious as to why the `*_t` items are keywords, but can't find anything about it. – R2-Dequeue Aug 04 '15 at 17:35
  • @R2-Dequeue, good thinking to summarise. – chr Aug 05 '15 at 14:47
  • @R2-Dequeue. Should I list keywords with an underscore in the question for completeness? Regarding e.g. `char16_t` and the `*_t`, perhaps there's a hint in the answer to [this question](http://stackoverflow.com/questions/12789376/are-char16-t-and-char32-t-misnomers): namely striving for a complete set of data types to represent "character data". Reading that answer got me thinking that maybe `char` ought to have been `char8_t` for consistency. But anyone so inclined could defined their own typedef. – chr Aug 05 '15 at 14:54
  • @chr I don't think it's a big deal, but do you. As for that question, thanks for the link, that answers a lot but leaves room for more questions. – R2-Dequeue Aug 05 '15 at 18:01

1 Answers1

3

From the original proposal for nullptr:

  • Programmers have often requested that the null pointer constant have a name, and nullptr appears to be the least likely of the alternative text spellings to conflict with identifiers in existing user programs. For example, a Google search for nullptr cpp returns a total of merely 150 hits, only one of which appears to use nullptr in a C++ program.

    • The alternative name NULL is not available. NULL is already the name of an implementation-defined macro in the C and C++ standards. If we defined NULL to be a keyword, it would still be replaced by macros lurking in older code. Also, there might be code “out there” that (unwisely) depended on NULL being 0. Finally, identifiers in all caps are conventionally assumed to be macros, testable by #ifdef, etc.

    • The alternative name null is impractical. It is nearly as bad as NULL in that null is also a commonly used in existing programs as an identifier name and (worse) as a macro name. For example, a Google search for null cpp returns about 180,000 hits, of which an estimated 3% or over 5,000 use null in C++ code as an identifier or as a macro.

  • Any other name we have thought of is longer or clashes more often.

(emphasis mine).

R2-Dequeue
  • 638
  • 1
  • 5
  • 15
  • 1
    You forgot the *"Our informal polling suggests that people seem to like nullptr"* item. That's the most important one. – DanielKO Aug 04 '15 at 01:47
  • Answer accepted and partly expected:-). Loved Howard's comment on running out of underscores! – chr Aug 04 '15 at 14:14
  • @DanielKO: Thanks. Full text of bullet item from p. 5 of the proposal: _Our informal polling suggests that people seem to like nullptr. If nothing else, it is the spelling that has elicited the fewest strong objections to date in our experience._ – chr Aug 04 '15 at 14:40