How to implement the CRTP following MISRA C++

  • A+

My team is developing a embedded system where we need to follow MISRA C++.

We are refactoring the code to use less virtual methods so we are trying to implement the CRTP to use static polymorphism instead of the dynamic one.

But we have the problem that static polymorfism requires a pointer conversion, so our static analysis checker complains.

Here is the interface

template <typename T> class UpdateMethod { protected:     ~UpdateMethod() {}  public:     void operator()() const     {         // [MISRA Rule 5-2-7] violation:         static_cast<const T*>(this)->update();     } }; 

Here is one of the implementations:

class A     : public UpdateMethod<A> {  public:     void update() const {} }; 

When in pass the MISRA checker it complains about the static_cast (conversion from ptr to ptr (e926).

So, my question is: is there any good way to implement the CRTP without having to suppress the MISRA warning, so in a secured way?

A related question only about the pointer conversion: MISRA C++ 2008 Rule 5-2-7 violation: An object with pointer type shall not be converted to an unrelated pointer type, either directly or indirectly I have the same error in the CRTP.

Edit: As mentioned only C++03 and no external library like boost.


You might use the reverse approach:

template <typename T> class UpdateMethod : public T {  public:     void operator()() const     {         this->update();     } };  class A_impl {  public:     void update() const {} };  typedef UpdateMethod<A_impl> A; 


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: