Main Content

AUTOSAR C++14 Rule A5-10-1

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

Since R2020b

Description

Rule Definition

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

Rationale

A call to a member virtual function is resolved by the compiler at run-time to the most derived version of the function. If you use the equality operators (==) or (!=) to compare anything other than the null-pointer constant nullptr with a pointer to a member virtual function, the result is unspecified.

Polyspace Implementation

Polyspace® flags any (==) or (!=) comparison where one operand is a pointer to a member virtual function and the other operand is not nullptr.

Troubleshooting

If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

class Base
{
public:
    virtual void f();
    void g();
};

template<typename T>
class Derived : public Base
{
public:
    void f();
};

void f()
{

    bool b = (&Derived<int>::f == &Derived<int>::f); // Noncompliant

    void (Derived<float>::* p)() = &Derived<float>::f;
    bool b1 = (&Derived<float>::f == p); // Noncompliant
    bool b2 = (p == p); // Noncompliant
    bool b3 = (p == nullptr); // Compliant

    void (Base::* q)() = &Base::g;
    bool b4 = (q == q); // Compliant

    void (Base::* r)() = &Base::f;
    bool b5 = (q == r); // Noncompliant

}

In this example, the result of the comparison in boolean b is non-compliant because the operands point to a member virtual function. Similarly, pointers p and r are pointers to member declarators that point to a member virtual function and Polyspace flags their use in equality comparison operations, except for the comparison of p to nullptr.

Check Information

Group: Expressions
Category: Required, Automated

Version History

Introduced in R2020b