1

I have read What exactly is nullptr? and I believe I understand the reasons why #defining NULL as 0 is/was problematic, and why nullptr's semantics are preferable.

I'm sure this was discussed, but I can't find an SO question on it: Why did C++11 not instead introduce a new keyword __nullptr of type nullptr_t, and then require the definition:

#define NULL __nullptr

?

I suspect I'm missing something, but it seems to me this would have achieved everything that the current nullptr does, but without requiring everyone to rewrite all their old, NULL-using code to use nullptr instead. It seems to me this would aid C/C++ cross-compatibility (yes, I know, they're different languages), and would have made it easier (during the transition period) for people trying to simultaneously use C++11 and pre-C++11 compilers.

Retroactively redefining NULL to be something other than the old, ambiguous "integer constant with value 0" would certainly have broken some amount of existing code, code that depended on NULL expanding to some version of 0. I would have thought that would have been worth it: I would have thought that preserving the historical usage of NULL as the null pointer constant would have been well worth the cost of rendering some old, poorly written code, that made ill-guided assumptions, invalid. But that course wasn't taken.

So if anyone knows: Was that course rejected because it would have broken too much old code, or code that someone felt deserved not to be broken? Or was there some other reason?

Steve Summit
  • 38,132
  • 7
  • 61
  • 86
  • Ah, so there was a question on it. Thanks. (I *did* look. I am disappointed in SO's search function for not finding it.) – Steve Summit Aug 16 '21 at 14:15
  • `without requiring everyone to rewrite all their old, NULL-using code to use nullptr instead` C++11 doesn't require anyone to rewrite their old NULL-using code. NULL is still in the standard library. There's simply no reason to use it when writing new code. – eerorika Aug 16 '21 at 14:19
  • ...and the [accepted answer](https://stackoverflow.com/questions/32302615/why-not-call-nullptr-null/32302644#32302644) is utter, utter garbage. :-\ But that's not SO's fault, or Baum mit Augen's fault. Sheesh. – Steve Summit Aug 16 '21 at 14:19
  • @eerorika The probably isn't the place for a discussion on this, but given that C++11 *does* say that `nullptr` is the preferred NULL pointer constant, I believe any competently-written C++ style guide would want to require using it, and would deprecate `NULL`, because having a mixture would just introduce a new layer of confusion. – Steve Summit Aug 16 '21 at 14:23
  • 3
    `NULL` is allowed to be defined as `nullptr`. Microsoft looked into doing that at some point, but found there was too much bad code broken within their own codebase let alone outside iirc. – chris Aug 16 '21 at 14:25
  • @chris It's hard to soar with the eagles when you're surrounded by turkeys. `NULL` has been so confusing for so many years (I know — I wrote the [C FAQ list section](http://c-faq.com/null/index.html) about it), and redefining it as `__nullptr` would have fixed it forever. But it was not to be. – Steve Summit Aug 16 '21 at 14:29
  • My 2 cents. In our company's C++ code base, I indeed redefined `NULL` as `nullptr`. – prapin Aug 16 '21 at 18:27

0 Answers0