AUTOSAR C++14 Rule A5-2-1
Description
Rule Definition
dynamic_cast should not be used.
Rationale
You use dynamic_cast
to convert the type of a pointer or reference to
a class along the inheritance hierarchy, for instance to convert a pointer to base class
into a pointer to a derived class. The conversion incurs an overhead due to the type
checking that is performed at run-time. This overhead is unsuitable for the low memory,
speed, and predictable performance requirements of real-time systems.
If you cannot avoid dynamic casting in your application, consider using a custom implementation to perform the cast. You might also consider using virtual functions if you are casting to the most derived class, or static polymorphism with overloaded functions and templates. In the latter case, the types are resolved at compile-time which avoids the overhead.
Polyspace Implementation
Polyspace® flags all uses of dynamic_cast
in your code.
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: Advisory, Automated |
Version History
Introduced in R2020b