Why is this an issue?
Before C++11, the only way to refer to a null pointer was by using the integer literal 0
, which created ambiguity with regard to
whether a pointer or an integer was intended. Even with the NULL
macro, the underlying value is still 0
.
C++11 introduced the keyword nullptr
, which is unambiguous and should be used systematically.
Noncompliant code example
void f(char *c);
void g(int i);
void h()
{
f(0); // Noncompliant
f(NULL); // Noncompliant
g(0); // Compliant, a real integer
g(NULL); // Noncompliant, NULL should not be used for a real integer
}
Compliant solution
void f(char *c);
void g(int i);
void h()
{
f(nullptr); // Compliant
g(0); // Compliant, a real integer
}
Resources