AutosarC++17_03-A4.10.1

Only nullptr literal shall be used as the null-pointer-constant

Required inputs: IR

Rationale

In C++, the literal NULL is both the null-pointer-constant and an integer type. To meet developer expectations, only nullptr pointer literal shall be used as the null-pointer- constant.

Note that, nullptr pointer literal allows parameters forwarding via a template function.

Example
//% $Id: A4-10-1.cpp 271715 2017-03-23 10:13:51Z piotr.tanski $
#include <cstddef>
#include <cstdint>

void f1(std::int32_t);
void f2(std::int32_t *);
void f3() {
f1(0);    // Compliant
f1(NULL); // Non-compliant - NULL used as      an integer,
// compilable
// f1(nullptr); // Non-compliant - nullptr     used as an integer
// compilation error
f2(0);       // Non-compliant - 0 used as     the null posinter constant
f2(NULL);    // Non-compliant - NULL used     as the null pointer constant
f2(nullptr); // Compliant
}
void f4(std::int32_t *);
template <class F, class A> void f5(F f, A a) { f4(a); }
void f6() {
// f5(f4, NULL); // Non-compliant - function f4(std::int32_t) not declared
f5(f4, nullptr); // Compliant
}
See Also
  • HIC++ v4.0 [8]: 2.5.3 Use nullptr for the null pointer constant

Possible Messages

Key

Text

Severity

Disabled

null_constant

Only nullptr literal shall be used as the null-pointer-constant.

None

False

zero_as_null

Use of literal zero (0) as null-pointer-constant, use {} instead

None

False

Options

require_nullptr

require_nullptr : bool = True

Require C++11 nullptr instead of macro NULL or literal zero (0).