NULL vs. nullptr: The Evolution of Null Pointer Representation
In C 0x, the traditional NULL macro was replaced by nullptr. This change was not merely cosmetic; it introduced significant improvements to the language's handling of null pointers.
Why the Replacement?
NULL, defined as 0, created a potential ambiguity in overloaded function resolution. Consider the following code:
void f(int); void f(foo *);
If a call to f(NULL) is made, the compiler is unsure whether to invoke f(int) or f(void *). This ambiguity is resolved by using nullptr, which has type std::nullptr_t and is implicitly convertible to any pointer type.
Benefits of nullptr
Compared to NULL, nullptr offers several advantages when working with pointers:
The above is the detailed content of NULL vs. nullptr: Why Was C 's Null Pointer Representation Changed?. For more information, please follow other related articles on the PHP Chinese website!