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

Concise syntax should be used for concatenatable namespaces

    XMLWordPrintable

    Details

    • Default Severity:
      Minor
    • Impact:
      Low
    • Likelihood:
      Low
    • Default Quality Profiles:
      Sonar way, MISRA C++ 2008 recommended
    • Covered Languages:
      C++
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      5min
    • Analysis Scope:
      Main Sources, Test Sources

      Description

      Namespaces represent a cross-file named scope. They are very useful to organize code and interfaces without cluttering a unique namespace. For instance, they provide a much cleaner way to avoid name collisions than using bad long names.

      Namespaces can be nested to provide even more structure to type and symbol names. In that case, namespaces can be nested one inside another like scopes would with curly braces.

      In C+17, a new concise syntax was introduced to increase the readability of nested namespaces. It is much less verbose and involves much less curly braces-delimited scopes. Whereas declaring a nested namespace of depth N requires N pairs of curly braces with the original syntax, this new syntax requires only one pair of curly braces. This syntax is much more readable and less error-prone. When possible, non-inlined or inlined (since C+20) named namespaces should be concatenated.

      Noncompliant Code Example

      namespace geometry { // Noncompliant
        namespace common {
          class point {
          };
        }
      }
      
      namespace geometry { // Noncompliant since C++20
        inline namespace triangle  {
          class edge {
          };
        }
      }
      

      Compliant Solution

      namespace geometry::common {
        class point {
        };
      }
      
      namespace geometry::inline triangle { // C++20
        class edge {
        };
      }
      
      namespace sonarsource { // Compliant: cannot be concatenated
        namespace core {
          class Rule {
          };
        }
        class A {
        };
      }
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              geoffray.adde Geoffray Adde
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated: