lundi 16 août 2021

Why did C++11 replace NULL with nullptr? [duplicate]

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?

Aucun commentaire:

Enregistrer un commentaire