C and C++


LANG.OPS.OVERLOADS : Confusing Operator Overload

Summary

[C++ only] One of the following operators is overloaded and the overloaded version is used one or more times.

|| logical and
&& logical or
, comma
unary & address-of

Properties

Class Name Confusing Operator Overload
Significance style
Mnemonic LANG.OPS.OVERLOADS
Categories
AUTOSARC++14 AUTOSARC++14:M5-2-11 The comma operator, && operator and the || operator shall not be overloaded.
  AUTOSARC++14:M5-3-3 The unary & operator shall not be overloaded.
MisraC++2008 MisraC++2008:5-2-11 The comma operator, && operator and the || operator shall not be overloaded.
  MisraC++2008:5-3-3 The unary & operator shall not be overloaded.
MisraC++2023 MisraC++2023:16.5.1 The logical AND and logical OR operators shall not be overloaded
  MisraC++2023:16.5.2 The address-of operator shall not be overloaded
CWE CWE:783 Operator Precedence Logic Error
JSF++ JSF++:159 Operators ||, &&, and unary & shall not be overloaded.
Availability Available for C and C++.
Enabling Checks for this warning class are disabled by default. To enable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += allow class="Confusing Operator Overload"

Example

class MyClass{
public:
   bool isTrue() const;
   MyClass add(const MyClass & b) const;
   // ... 

   MyClass * operator&() const{                            /* 'Confusing Operator Overload' warning issued here */
                                                           /* (definition can be inline) */
      return (MyClass *)10000;
   }

   bool operator!() const{                                                      /* not one of the specified operators */
      return !(this->isTrue());
   }
};


bool operator||(const MyClass & lhs, const MyClass & rhs){ /* 'Confusing Operator Overload' warning issued here */
                                                           /* (definition can be out of line) */
   return lhs.isTrue() || rhs.isTrue();
}

bool operator&&(const MyClass & lhs, const MyClass & rhs){ /* 'Confusing Operator Overload' warning issued here */
   return lhs.isTrue() && rhs.isTrue();
}

int operator,(const MyClass & lhs, const MyClass & rhs){   /* 'Confusing Operator Overload' warning issued here */
   return 5;
}

MyClass operator+(const MyClass & lhs, const MyClass & rhs){                     /* not one of the specified operators */
   return lhs.add(rhs);
}

void lang_ops_overloads(MyClass a){
   &a;
   !a;
   a||a;
   a&&a;
   a,a;
   a+a;
}

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.