AutosarC++19_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 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
    #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 [9]: 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