AUTOSAR C++14 Rule A13-5-1
If "operator[]" is to be overloaded with a non-const version, const version shall also be implemented
Description
Rule Definition
If "operator[]" is to be overloaded with a non-const version, const version shall also be implemented.
Rationale
Typically, you overload the subscript operator operator[] to provide
read and write access to individual elements of an array or similar structure contained in a
class. If you implement a non-const overload of
operator[], you must also implement a const version
of this overload. Otherwise, you cannot use operator[] to read elements
of a const object.
This rule allows the implementation of a const overload of
operator[] for read-only access without the corresponding
non-const overload.
Polyspace Implementation
Polyspace® flags the definition of the non-const member function if no
corresponding const version of the member function is implemented.
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, Automated |
Version History
Introduced in R2020a