Overloaded &&, ||, or comma operator
ID |
c.maintainability.overload_short_circuit_operator |
Severity |
low |
Remediation Complexity |
hard |
Remediation Risk |
high |
Remediation Effort |
medium |
Resource |
Operator Overload |
Language |
C / C++ |
Description
This overloads &&, ||, or the comma operator. As a function call, an overloaded operator always evaluates both operands before the call happens, so it loses the short-circuit evaluation of &&/|| and the left-to-right sequencing the comma operator normally guarantees. Code that relies on the built-in behavior — skipping a null check, ordering two side effects — silently breaks for this type. Use a named function instead of overloading these operators.
Rationale
This overloads &&, ||, or the comma operator. As a function call, an overloaded operator always evaluates both operands before the call happens, so it loses the short-circuit evaluation of &&/|| and the left-to-right sequencing the comma operator normally guarantees. Code that relies on the built-in behavior — skipping a null check, ordering two side effects — silently breaks for this type. Use a named function instead of overloading these operators.
The following code illustrates the pattern detected by this rule:
struct Expr {
// FLAGGED: Overloaded &&, ||, or comma operator
bool operator&&(const Expr &other) const { return true; }
};