title: Does MISRA C++ rule 7-1-1 affect references?
body: <blockquote>
    <h3>Rule 7-1-1 (Required) A variable which is not modified shall be const qualified</h3>
    
    <p>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.</p>
  
  <pre><code>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;
  }
  </code></pre>
  </blockquote>
  
  <p>The example included in the standard is focused on pointers. The rule requires all the pointers meeting the conditions to be <code>const</code>, e.g. <code>int * const</code>. If I understand it correctly, it <em>does not</em> require pointers and references to point to <code>const</code> objects, e.g. <code>const int *</code> or <code>const int &amp;</code>. In fact, it is covered by another rule (but only for parameters!):</p>
  
  <blockquote>
    <p>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</p>
  </blockquote>
  
  <p>So, does the rule 7-1-1 apply to references at all? A reference cannot be re-bound after it has been created, so it should be treated like a <code>const</code> pointer. Therefore all references should automatically comply with the rule 7-1-1.</p>
  
  <p><em><strong>EDIT</strong> (based on comments by <a href="https://stackoverflow.com/users/560648/lightness-races-in-orbit">Lightness Races in Orbit</a>, <a href="https://stackoverflow.com/users/3370124/richard-critten">Richard Critten</a> &amp; <a href="https://stackoverflow.com/users/4706785/peter">Peter</a> and my experiments):</em> Or does the rule apply to the type of referenced object in the case of references? I mean <code>const int &amp;</code> vs. <code>int &amp;</code> similarly to <code>const int</code> vs. <code>int</code>?
  I am asking because my MISRA C++ checker keeps reporting violations on references… Example of its behavior:</p>
  
  <pre><code>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&amp; const_ref_A = nonconst_obj_A;
      A&amp; nonconst_ref_A = nonconst_obj_A; // OK: setter called
      nonconst_ref_A.set_property(4);
      ConstA&amp; const_ref_constA = nonconst_obj_constA; // violation: no modification
      // In fact, it seems to be impossible to make
      // a non-violating ConstA&amp; 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&amp; const_ref_A = nonconst_obj_A;
      A&amp; nonconst_ref_A = nonconst_obj_A; // violation: no modification using the ref.
      const ConstA&amp; const_ref_constA = nonconst_obj_constA;
  }
  </code></pre>