C++ MISRA C++;规则7-1-1影响参考? 规则7-1-1(必需)未修改的变量应为常量

C++ MISRA C++;规则7-1-1影响参考? 规则7-1-1(必需)未修改的变量应为常量,c++,reference,misra,C++,Reference,Misra,如果变量不需要修改,则应 使用常量限定声明,因此无法修改。A. 然后,非参数变量将需要在 声明要点。此外,未来的维护也不会意外 修改该值 void b ( int32_t * ); int32_t f ( int32_t * p1, // Non-compliant int32_t * const p2, // Compliant int32_t * const

如果变量不需要修改,则应 使用常量限定声明,因此无法修改。A. 然后,非参数变量将需要在 声明要点。此外,未来的维护也不会意外 修改该值

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
,例如
int*const
。如果我理解正确,它不需要指向
const
对象的指针和引用,例如
const int*
const int&
。事实上,它包含在另一条规则中(但仅适用于参数!):

规则7-1-2(必选)如果未修改相应对象,函数中的指针或引用参数应声明为指向常量的指针或指向常量的引用

那么,规则7-1-1是否适用于参考文献?引用创建后无法重新绑定,因此应将其视为
const
指针。因此,所有参考文件应自动符合规则7-1-1

编辑(基于,&和我的实验的评论):或者在引用的情况下,规则是否适用于引用对象的类型?我的意思是
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;
}

< C++指针与引用之间存在显著的语义差异:引用不是对象(变量)。
这只是对象别名。因此,严格意义上,7-1-1不适用于参考文献。

不,7-1-1不适用于参考文献

形式为
int32\u t&const p2
的声明是无意义的

参考文献唯一有意义的
const
限定形式为
const int32_t&p2
或等效的
int32_t const&p2
。7-1-2完全涵盖了这些表格的需求


Misra 7-1-1不需要应用于此类引用,因为Misra的理念是指定语言标准没有规定的约束,而不是重述语言中已经指定(并由编译器强制执行)的约束。Misra 7-1-1要求声明
const
类型的变量(例如
int32_t
),前提是该变量不会被修改-例如引用的示例中的
i
。在标准C++中,不可能创建一个非->代码> const <代码>引用该变量——除非使用类型转换(AKA“Casic”)来删除<代码> const < /Cord>Misra规则5-2-5要求不得使用此类强制转换。

它说的是“变量”,所以我的理解是,如果引用可以是常量,那么应该是常量。如果您的问题是关于引用的,那么请提供有问题的代码和引用。。。我认为这条规则很弱。而且,即使它是相关的,它也会像问题已经说明的那样隐含地适用。所以我看不出7-1-1是怎么应用的。7-2-2是相关规则,但奇怪的是,仅适用于函数paramsAgree,规则有点模糊。7-1-1是关于变量的,但示例描述了函数参数。目前还不清楚“变量”一词是否包括参数,以及在7-1-2中区分它们的目的是什么。几乎但不完全-7-1-2只包括参数,除非有OP没有的文本quote@LightnessRacesinOrbit你是对的。规则7-1-2说:这条规则使函数接口的定义更加精确。所以它不会影响局部变量。这使得7-1-1更值得怀疑…@Melebius:更奇怪的是,它接着说,这意味着指针对象应该是
const
,而不是指针本身。这真让人困惑。就像谷歌的C++风格指南一样,我坚持那些坚持MISRA规则的人,就像他们是法律一样。或者,好吧,好像他们是明智的。@LightnessRacesinOrbit谢谢你的评论,我已经完善了这个问题。Misra通常不陈述语言中的要求。如果对象是代码> const ,如果意图不修改,则7-1—1需要,那么在C++中不可能创建一个非->代码> const 引用,除非使用类型转换(AKA“Casic”)来删除<代码> const 限定符。C++编译器将诊断错误。虽然可以通过类型转换强制编译器提交,但另一条Misra规则(5-2-5)要求不得使用此类强制转换。我将很快编辑并添加一条评论。