Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-3708

Reference types should not be qualified with "const" or "volatile"

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Remove this cv-qualifier.
    • Highlighting:
      Hide

      const

      Show
      const
    • Default Severity:
      Minor
    • Impact:
      Low
    • Likelihood:
      Low
    • Default Quality Profiles:
      Sonar way, MISRA C++ 2008 recommended
    • Covered Languages:
      C, C++
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      2min
    • Analysis Scope:
      Main Sources, Test Sources
    • CERT:
      DCL52-CPP.

      Description

      The C++ specification forbids the qualification of reference types with const or volatile unless it happens via a typedef, in which case it's ignored. Most compilers treat such direct qualifications as errors, but the Microsoft compiler allows them.

      This rule raises an issue on both types of const qualification.

      Noncompliant Code Example

      void example(char c) {
        char & const direct = c; // Noncompliant
      
        typedef char & T;
        const T indirect = c; // Noncompliant
      }
      

      Compliant Solution

      void example(char c) {
        char & direct = c; // or: const char & direct = c;
      
        typedef char & T;
        T indirect = c;
      }
      

      See

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              ann.campbell.2 Ann Campbell
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated: