【发布时间】:2018-10-11 08:42:22
【问题描述】:
我的团队正在开发一个嵌入式系统,我们需要遵循 MISRA C++。
我们正在重构代码以使用更少的虚拟方法,因此我们尝试实现 CRTP 以使用静态多态性而不是动态多态性。
但是我们有静态多态需要指针转换的问题,所以我们的静态分析检查器抱怨。
界面如下
template <typename T>
class UpdateMethod
{
protected:
~UpdateMethod() {}
public:
void operator()() const
{
// [MISRA Rule 5-2-7] violation:
static_cast<const T*>(this)->update();
}
};
这是其中一种实现方式:
class A
: public UpdateMethod<A>
{
public:
void update() const {}
};
当通过 MISRA 检查器时,它会抱怨 static_cast(从 ptr 转换为 ptr (e926)。
所以,我的问题是:有没有什么好的方法来实施 CRTP 而不必抑制 MISRA 警告,所以以一种安全的方式?
仅关于指针转换的相关问题: 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 我在 CRTP 中有同样的错误。
编辑:仅提到 C++03,没有像 boost 这样的外部库。
【问题讨论】:
标签: c++ c++03 crtp static-cast misra