MISRA C++:2023 Rule 16.5.1
Description
Rule Definition
The logical AND and logical OR operators shall not be overloaded.
Rationale
When you overload an operator, the overloaded operator behaves as a function call. The
comma operator, the logical AND (&&
) operator, and the logical OR
(||
) operator have certain behaviors that cannot be replicated by their
overloaded counterpart. While the C++ standard specifies the order of evaluation of
overloaded operators, the order of evaluation of their operands is not specified. A compiler
that short circuits the builtin &&
or ||
operators might not do the same for an overloaded version of these operators.
Overloading these operators creates confusion about how these operators behave. Avoid
overloading the comma operator, the &&
operator, and the
||
operator.
Polyspace Implementation
Polyspace® flags the overloading of these operators:
Comma operator
&&
operator||
operator
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
Check Information
Group: Overloading |
Category: Required |
Version History
Introduced in R2024b