AutosarC++18_03-A13.5.4ΒΆ

If two opposite operators are defined, one shall be defined in terms of the other

Required inputs: IR

Rationale

Defining one operator in terms of the other simplifies maintenance and prevents from accidental errors during code development.

Note: Completeness of relational operators can be achieved by implementing just operator== and operator< and using namespace rel_ops.

Example
// $Id: A13-5-4.cpp 305629 2018-01-29 13:29:25Z piotr.serwa $
#include <cstdint>

// non-compliant
class A {
public:
  explicit A(std::uint32_t d) : d(d) {}
  bool operator==(const A &rhs) const noexcept { return d == rhs.d; }
  bool operator!=(const A &rhs) const noexcept { return d != rhs.d; }

private:
  std::uint32_t d;
};

// compliant
class B {
public:
  explicit B(std::uint32_t d) : d(d) {}
  bool operator==(const B &rhs) const noexcept { return d == rhs.d; }
  bool operator!=(const B &rhs) const noexcept { return !(*this == rhs); }

private:
  std::uint32_t d;
};
See Also
  • JSF December 2005 [7]: AV Rule 85: When two operators are opposites (such as == and !=), both will be defined and one will be defined in terms of the other.

Possible Messages

Key

Text

Severity

Disabled

implement_in_terms

Implement in terms of other operator

None

False

Options