AutosarC++17_03-A5.3.1

Evaluation of the operand to the typeid operator shall not contain side effects

Required inputs: IR

Rationale

The operand of typeid operator is evaluated only if it is a function call which returns a reference to a polymorphic type.

Providing side effects to typeid operator, which takes place only under special circumstances, makes the code more difficult to maintain.

Example
// $Id: A5-3-1.cpp 271696 2017-03-23 09:23:09Z piotr.tanski $
#include <typeinfo>
bool sideEffects() noexcept {
// Implementation
return true;
}
class A {
public:
static A &f1() noexcept { return a; }
virtual ~A() {}

private:
static A a;
};
A A::a;
void f2() noexcept(false) {
typeid(sideEffects()); // Non-compliant - sideEffects() function not called
typeid(A::f1()); // Non-compliant - A::f1() functions called to determine
           // the polymorphic type
}
See Also
  • HIC++ v4.0 [8]: 5.1.6 Do not code side effects into the right-hand operands of: &&, ||, sizeof, typeid or a function passed to condition_variable::wait.

Possible Messages

Key

Text

Severity

Disabled

side_effect_in_typeid

Operand of “typeid” shall not contain side effects

None

False

Options