
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
cpp
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
cpp
void f(char *c);
void g(int i);
void h()
{
f(nullptr); // Compliant
g(0); // Compliant, a real integer
}
See
- C++ Core Guidelines ES.47 - Use nullptr rather than 0 or NULL
For example
