AUTOSAR C++14 Rule A5-3-1
Evaluation of the operand to the typeid operator shall not contain side effects
Since R2020b
Description
Rule Definition
Evaluation of the operand to the typeid operator shall not contain side effects.
Rationale
The typeid
operator evaluates its operand only if it is a call to a
function that returns a reference to a polymorphic type (a polymorphic type is a class type
that contains virtual functions). In all other cases, expressions provided to the
typeid
operator are not evaluated.
For code that is easier to maintain, avoid expressions with side effects altogether when
using the typeid
operator. You or another developer will be saved from
tracking down the ingredients of the expression to their definitions and determining if the
side effects actually occur.
Polyspace Implementation
The checker flags typeid
operators with expressions that have side
effects. Function calls are assumed to have side effects.
The checker message states whether the expression is evaluated or ignored. If the
expression is evaluated and you want to retain the expression in the
typeid
operation instead of performing the evaluation in a separate
statement, add comments to your result or code to avoid another review. See:
Address Results in Polyspace User Interface Through Bug Fixes or Justifications if you review results in the Polyspace user interface.
Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access) if you review results in a web browser.
Annotate Code and Hide Known or Acceptable Results if you review results in an IDE.
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: Expressions |
Category: Required, Non-automated |
Version History
Introduced in R2020b