NULL
is an implementation-defined macro that are consider problematic practice in C++.
Overloading
int f(int);
int f(int*);
f(NULL); // ambiguous
Worse:
int g(long);
int g(long*);
g(NULL); // guess who get called?
Optimization
A nullptr_t
overload can be slightly fast than a pointer overload if we know a pointer is NULL
at compile time.
nullptr
In C
- The proposal: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2394.pdf
- related: Type-generic macro
- “Conditional expressions such as
(1 ? 0 : NULL)
and(1 ? 1 : NULL)
have different status depending howNULL
is defined. Whereas the first is always defined, the second is a constraint violation ifNULL
has typevoid*
, and defined otherwise” - “A
NULL
argument that is passed to ava_arg
function that expects a pointer can have severe consequences. On many architectures nowadaysint
andvoid*
have different size, and so ifNULL
is just0
, a wrongly sized arguments is passed to the function.”