BullseyeCoverage Up Contents Search

Error on Expression Using Overloaded Logical Operator

SYMPTOMS

Microsoft C++ error C2440: '?' : cannot convert from 'type' to 'bool'
GNU C++ error: could not convert 'type' to 'bool'
Clang error: value of type 'type' is not contextually convertible to 'bool'

CAUSE

A declaration of operator&& or operator|| is incompatible with operands or a result of type bool. Defining these operators with operand type or return type other than bool conflicts with condition/decision coverage at a basic level, and is not compatible with BullseyeCoverage.

RESOLUTION

It is recommended to first try the latest release .

Alternative options are listed below in order of preference.

  1. Rename the overloaded operators && and || to conventional function names.
  2. Overload operator& or operator| rather than operator&& or operator||.
  3. Modify your source code to invoke the overloaded operators using the names operator&& and operator||. For example,
    // a = b && c;              ← change this
    a = b.operator&&(c);        ← to this
    
  4. Modify the source code so that instead of defining operator&& or operator||, instead define operator bool() to convert all expressions to boolean values, and then use the built-in && and || operators on the boolean values. For example,
    class C {
    public:
    	//bool operator&&(const C&) const;      ← remove this
    	//bool operator||(const C&) const;      ← remove this
    	operator bool() const;                  ← add this
    };
    
    C c1, c2;
    if (c1 && c2)
    if (c1 || c2)
    
  5. Exclude functions that invoke overloaded operator&& or operator||.

MORE INFORMATION

For details on how BullseyeCoverage instruments overloaded operator&& and operator||, see Measurement Technique.

See also Best Practice Recommendations.

Updated: 14 Sep 2023