c++ - MISRA C++ 规则 7-1-1 会影响引用吗?

标签 c++ reference misra

Rule 7-1-1 (Required) A variable which is not modified shall be const qualified

If a variable does not need to be modified, then it shall be declared with const qualification so that it cannot be modified. A non-parametric variable will then require its initialization at the point of declaration. Also, future maintenance cannot accidentally modify the value.

void b ( int32_t * );
int32_t f (       int32_t * p1,                 // Non-compliant
                  int32_t * const p2,           // Compliant
                  int32_t * const p3 )          // Compliant
{
     *p1 = 10;
     *p2 = 10;
     b( p3 );
     int32_t i = 0;                             // Non-compliant
     return i;
}

标准中包含的示例侧重于指针。该规则要求所有满足条件的指针都是const,例如整数 * 常量。如果我理解正确,它不需要需要指针和引用来指向 const 对象,例如const int *const int &。事实上,它被另一条规则所涵盖(但仅限于参数!):

Rule 7-1-2 (Required) A pointer or reference parameter in a function shall be declared as pointer to const or reference to const if the corresponding object is not modified

那么,规则 7-1-1 是否适用于引用文献?引用创建后不能重新绑定(bind),因此应将其视为 const 指针。因此,所有引用都应自动遵守规则 7-1-1。

编辑(基于 Lightness Races in OrbitRichard CrittenPeter 的评论以及我的实验): 或者规则是否适用于引用对象的类型在引用的情况下?我的意思是 const int & vs. int & 类似于 const int vs. int? 我问是因为我的 MISRA C++ 检查器不断报告引用违规……其行为示例:

class A
{
    int property;
public:
    A(int param) : property(param) {}     // violation: should be: const int param
    int get_property() const { return property; }
    void set_property(int param) { property = param; }  // violation: should be: const int param
};

class ConstA
{
    const int property;
public:
    ConstA(int param) : property(param) {}  // violation: should be: const int param
    int get_property() const { return property; }
    // setter not allowed
};

void example1()
{
    const A const_obj_A(1);
    A nonconst_obj_A(2);
    ConstA nonconst_obj_constA(3);      // OK: used to create a non-const reference
    const A& const_ref_A = nonconst_obj_A;
    A& nonconst_ref_A = nonconst_obj_A; // OK: setter called
    nonconst_ref_A.set_property(4);
    ConstA& const_ref_constA = nonconst_obj_constA; // violation: no modification
    // In fact, it seems to be impossible to make
    // a non-violating ConstA& declaration.
    // The only chance is to make another non-const reference
    // but the last declaration in the chain will still violate.
}

void example2()
{
    const A const_obj_A(1);
    A nonconst_obj_A(2);
    ConstA nonconst_obj_constA(3);      // violation: used only in const reference
    const A& const_ref_A = nonconst_obj_A;
    A& nonconst_ref_A = nonconst_obj_A; // violation: no modification using the ref.
    const ConstA& const_ref_constA = nonconst_obj_constA;
}

最佳答案

不,7-1-1 不适用于引用。

int32_t & const p2 形式的声明是无稽之谈。

引用的唯一有意义的 const 限定是 const int32_t &p2 或等效的 int32_t const &p2 形式。然后 7-1-2 中完全涵盖了对这些表格的需求。

Misra 7-1-1 不需要应用于此类引用,因为 Misra 的理念是指定语言标准没有的约束,而不是重申语言中已经指定的约束(并由编译器强制执行)。 Misra 7-1-1 要求将变量(例如 int32_t 类型)声明为 const 如果它不会被修改 - 例如 i 在引用的示例中。在标准 C++ 中,不可能创建对该变量的非 const 引用 - 除非使用类型转换(也称为“强制转换”)来删除 constness . Misra 规则 5-2-5 要求不得使用此类强制转换。

关于c++ - MISRA C++ 规则 7-1-1 会影响引用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42388555/

相关文章:

c++ - 派生类是否间接继承基类的赋值运算符?

rust - 为什么不能在同一结构中存储值和对该值的引用?

c - 带有数组初始化器的 misra 19.10

c - 类似函数的宏, 'REPORT_ERROR',已定义 [MISRA 2012 指令 4.9,咨询]

c++ - 性能测量 : time vs tick?

C++ 如何声明指向二维数组的指针

c++ - 预处理器和空格规则

reference - 各种操作的大致 CPU 周期数

c++ - undefined reference ,但已定义

#include *not* 后面可以跟 <filename> 或 "filename"吗?