c++:需要正确的语法以避免 MISRA 14-6-1 警告。具有依赖基类的类模板

标签 c++ templates inheritance constructor misra

我为一种叫做 SML 的语言编写了一个词法分析器/解析器。它用于电子仪表。我正处于静态代码分析阶段。 Lint 正在报告 MISRA 违规行为 14-6-1。规则(必需)是:“在具有依赖基的类模板中,在该依赖基中可以找到的任何名称都应使用限定 ID 或 this-> 引用”

系统:Raspberry PI 3B,g++:cc 版本 4.9.2 (Raspbian 4.9.2-10),PC LINT v9.0k。语言:C++98

代码摘录:

class SmlElementBase
{
    public:
        SmlElementBase(void) {}
        virtual ~SmlElementBase(void) {}                    
        // Main "parse" pure function to be implemented / overwritten by     all derived classes
        virtual prCode parse(ParserContext &pc) = 0;        
        // Per default an SML Element is not a container
        virtual boolean isContainer(void) const { return false; }
};

template<const Token::TokenType tokenType, const TokenLength TokenLength=0UL>
class SmlPrimitive : public SmlElementBase
{
    public:
        SmlPrimitive(void) : SmlElementBase() {}
        virtual ~SmlPrimitive(void) {}
        virtual prCode parse(ParserContext &pc);    // Parse function     (calls match function)

    protected:      
        virtual boolean match(const Token *token);  // Match token with     expected type and length
};

template<typename ValueType, const Token::TokenType tokenType, const TokenLength tokenLength=0UL>
class SmlPrimitiveWithValue : public SmlPrimitive<tokenType, tokenLength>
{
    public:
        //lint -e{1960,915,919}
        SmlPrimitiveWithValue(void) : SmlPrimitive<tokenType, tokenLength>(), value() {}; 
        // SmlPrimitiveWithValue(void) : SmlPrimitive<tokenType, tokenLength>(), value::SmlPrimitive() {};     NO DIFFERENCE
        virtual ~SmlPrimitiveWithValue(void) {}
        // Parse function is inherited. 
        //lint -e{1925}
        ValueType value;    // Value will be stored here. Public because of visitor pattern
    protected:
        virtual ::boolean match(const ::Token *token); // Match token with expected type and length and store value
};

typedef SmlPrimitiveWithValue<u8, Token::UNSIGNED_INTEGER, 1UL>             Unsigned8;

Lint 报告如下:

SmlPrimitiveWithValue(void) : SmlPrimitive<tokenType, tokenLength>(), value() {}; 

注意 1942:由于依赖基类 [MISRA C++ 规则 14-6-1],不合格的名称“SmlPrimitive”容易被误解

对于 SmlPrimitiveWithValue 的构造函数。

避免违反 MISRA 规则的正确语法是什么?

我试了又试,但找不到解决方案。

编辑:

我也试过:

SmlPrimitiveWithValue(void) : ::SmlPrimitive<tokenType, tokenLength>::SmlPrimitive(), value() {}; 

这不会编译。编译器消息:

/home/pidata/project/ehz/include/parser.hpp: In constructor ‘ParserInternal::SmlPrimitiveWithValue<ValueType, tokenType, tokenLength>::SmlPrimitiveWithValue()’:
/home/pidata/project/ehz/include/parser.hpp:281:50: error: expected template-name before ‘<’ token
      SmlPrimitiveWithValue(void) : ::SmlPrimitive<tokenType, tokenLength>::SmlPrimitive(), value() {};
                                                  ^
/home/pidata/project/ehz/include/parser.hpp:281:50: error: expected ‘{’ before ‘<’ token

最佳答案

这有点傻,因为非限定名称(不用于类成员访问)是 not looked up in dependent bases at all .因此,与定义不同的专业解释不可能(比如说)格式错误的 NDR。

反正指南要的就是让你写

SmlPrimitiveWithValue(void) : ::SmlPrimitive<tokenType, tokenLength>(), value() {};

(使用包含 SmlPrimitive 的任何命名空间,如果有的话)。也就是说,用 qualified-id 命名基类。

关于c++:需要正确的语法以避免 MISRA 14-6-1 警告。具有依赖基类的类模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52132355/

相关文章:

c++ - 考虑 makecontext() 什么是 uc_stack.ss_size 好?

c++ - 移动语义和多重继承

C++ 对临时 : is it legal? 进行非常量引用

c++ - 如何更好地处理依赖于模板参数的类成员类型?

c++ - 继承后重新获得 move 可构造性

c++ - 为什么这个模板类不能正常工作?

c++ - 为什么在通过模板进行静态调度时不需要前向声明?

c++ - 表达式模板 - 无法专门化函数模板

swift - 检查继承类和 super 类

c++ - 为什么要使用虚拟基类?