AutosarC++17_03-A5.10.1

A pointer to member virtual function shall only be tested for equality with null-pointer-constant

Required inputs: IR

Rationale

The result of equality comparison between pointer to member virtual function and anything other than null-pointer-constant (i.e. nullptr, see: A4-10-1) is unspecified.

Example
// $Id: A5-10-1.cpp 271927 2017-03-24 12:01:35Z piotr.tanski $
class A {
public:
virtual ~A() = default;
void f1() noexcept {}
void f2() noexcept {}
virtual void f3() noexcept {}
};

void fn() {
bool b1 = (&A::f1 == &A::f2);  //   Compliant
bool b2 = (&A::f1 == nullptr); //   Compliant
bool b3 = (&A::f3 == nullptr); //   Compliant
bool b4 = (&A::f3 != nullptr); //   Compliant
bool b5 = (&A::f3 == &A::f1);  //   Non-compliant
}
See Also
  • HIC++ v4.0 [8]: 5.7.2 Ensure that a pointer to member that is a virtual function is only compared (==) with nullptr.
  • JSF December 2005 [7]: AV Rule 97.1 Neither operand of an equality operator (== or !=) shall be a pointer to a virtual member function.

Possible Messages

Key

Text

Severity

Disabled

member_virtual_function_comparison

Comparison of a member virtual function with non-nullptr.

None

False

Options

check_class_types

check_class_types : bool = False

Check class for virtual methods that may match a comparison argument.
 

require_nullptr

require_nullptr : bool = False

Disallow the integer 0 as null pointer literal.