MISRA C++:2023 Rule 16.5.1
Description
Rule Definition
The logical AND and logical OR operators shall not be overloaded. 1
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
1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.
The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:
MISRA C:2004
MISRA C:2012
MISRA C:2023
MISRA C++:2008
MISRA C++:2023
MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.