Why not call nullptr NULL?

In C ++ 11, the nullptr keyword was added as a null pointer constant of a safer type, since the previous general definition of NULL as 0 has some problems.

Why did the standards committee decide not to name the new null pointer constant NULL or declare that NULL should be #define d before nullptr ?

+67
c ++ c ++ 11 language-design
Aug 31 '15 at 0:46
source share
7 answers

Stefan T. Lavavey (member of the C ++ standard committee) explained that once in talk (55:35):

While #define NULL nullptr allowed for implementation, this may violate some uses, such as

 int i = NULL; 

and apparently there are a lot of them. Therefore, they could not force the change.

+70
Aug 31 '15 at 0:50
source share

nullptr has a pointer type , while NULL tends to be an integer, and sometimes in overloaded functions you need to understand that you are using a pointer and not an integer - this is when nullptr in handy.

Thus, to really answer your question, NULL and nullptr serve two different purposes, and redefining one to the other will probably break a lot of things in existing code bases.

Also check this out on Bjarn Straustrup's website :

Should I use NULL or 0?

In C ++, the definition of NULL is 0, so there is only an aesthetic difference. I prefer to avoid macros, so I use 0. Another problem with NULL is that people sometimes mistakenly think that it is different from 0 and / or not an integer. In the standard code, NULL was / is sometimes defined for something inappropriate and therefore should / should be avoided. This is less common these days. If you must name a null pointer, name it nullptr; what he called in C ++ 11. Then, "nullptr" will be the keyword.

+41
Aug 31 '15 at 0:53
source share

Without actually participating in the discussion on the standardization committee, it's hard to say for sure, but I would have thought, because he would have broken some code that uses NULL in a value where nullptr not compatible enough. And breaking old code is never a good idea.

+8
Aug 31 '15 at 0:49
source share

NULL not type safe. For historical reasons, it was defined as 0 without casting, and a warning of silence of the compiler about the number of castes for the pointer at this special zero.

In an instant you can do:

 void* p = 0; 

but not this without implicit casting:

 void* p = 1234; 

a side effect is that it can be abused as numerical values, as mentioned in another answer.

nullptr improve this by using a pointer, you cannot assign this integer. Because the behavior is changed, a new name is created for backward compatibility.

Also note that nullptr processed by the compiler, its actual value is not displayed to the user (for example, zero in case of NULL ). It is much easier to have an architecture-dependent value, say 0xdeadbeef , without changing the logic of the programmer's code.

+8
Aug 31 '15 at 1:06
source share

Why did the standards committee decide not to name the new null pointer constant NULL

Presumably because the new null pointer is a keyword, and the keywords cannot be #defined , so calling it NULL would make any C. header badly formed.

or declare that NULL must be #defined before nullptr ?

The standards committee allows NULL be #defined to nullptr , but it does not require it.

C ++ 11 18.2 Types [support.types] / 2 : The NULL macro is the C ++ null pointer constant defined by the implementation in this International Standard.

C ++ 11 4.10 Pointer conversions [conv.ptr] / 1 : The null pointer constant is an integer constant expression (5.19) prvalue of an integer type that evaluates to 0 or the value of the std::nullptr_t .

Backward compatibility is not a problem here; any use of NULL , assuming it is an integer form of 0 , is not a standard consonant. Implementations may prefer not to do so to justify such malicious behavior.

+2
Sep 09 '15 at 1:35
source share

I will demonstrate a case where the decision to define nullptr as another type helps prevent errors.

Consider the following functions:

 void foo(int); void foo(char *); int main() { foo(NULL); // oops } 

In C ++ 98, the above code calls the foo (int) function because NULL is replaced with 0, which is most likely not what you intended.

But if you call foo (nullptr) , it calls the correct one - foo (char *) .

+2
Sep 21 '15 at 14:10
source share

nullptr is introduced for type safety and for clarity (possibly to stop type initialization without pointers using NULL ).

NULL (int type) does not change to nullptr (pointer type) to avoid confusion and provide backward compatibility.

Thus, the standard train of thought is probably associated with a smooth transition from the old to the new notation, without causing ambiguity or inhibiting any existing code.

+1
Sep 07 '15 at 9:07
source share



All Articles