AutosarC++18_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 298086 2017-11-24 11:13:27Z michal.szczepankiewicz $
#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 pointer 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).