> If you ever see the macro NULL in code, be afraid. There are two valid ways of defining the macro
Not on a Posix system, where the only valid definition of it is `(void*)0`. C could have adopted this definition.
Nullptr is needed in C++ because `0` is the only definition of `NULL` that works with the type system, due to the lack of implicit `void*` conversions.
C doesn't have this problem.
Adopting the Posix definition of NULL in the standard would have been sufficient -- and unlike `nullptr`, would have solved bugs in existing programs.
> Nullptr is needed in C++ because `0` is the only definition of `NULL` that works with the type system, due to the lack of implicit `void` conversions.*
> C doesn't have this problem.
Except for conversions between data pointers and function pointers ;)
Initialization seems to be a special case, but with `-pedantic` the following code will show a warning on the initialization of `fp2`:
That's a fine general sentiment. However, in this context it's a problem if you want to assign NULL to a pointer without a cast, which is why C++ added the magically convertible nullptr in addition to the magically convertible `0` constant.
char *x = 0; // ok in C and C++
char *y = (void*)0; // ok in C, error in C++
char *z = nullptr; // ok in C++
therefore:
#define NULL ((void*)0) // Required by Posix C, invalid C++
#define NULL 0 // Pre-nullptr, the only valid C++ definition
C++ can't define NULL the safe way that Posix C does.
I don't understand why it's more acceptable to allow magic `0` conversions than magic `(void*)0` conversions, given that the latter is far less likely to happen by accident -- but here we are.
Not on a Posix system, where the only valid definition of it is `(void*)0`. C could have adopted this definition.
Nullptr is needed in C++ because `0` is the only definition of `NULL` that works with the type system, due to the lack of implicit `void*` conversions.
C doesn't have this problem.
Adopting the Posix definition of NULL in the standard would have been sufficient -- and unlike `nullptr`, would have solved bugs in existing programs.