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

'extern "C"' should not be used with namespaces

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Hide
      * Move this 'extern "C"' declaration out of the namespace.
      * Move this "namespace" out of the 'extern "C"' declaration.
      Show
      * Move this 'extern "C"' declaration out of the namespace. * Move this "namespace" out of the 'extern "C"' declaration.
    • Highlighting:
      Hide

      inner declaration (see code samples)

      Show
      inner declaration (see code samples)
    • 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:
      5min
    • Analysis Scope:
      Main Sources, Test Sources

      Description

      The C linkage declaration extern "C" can not be combined with a namespace. In practical terms only one function with that name can be declared as extern "C" because the namespace is functionally ignored.

      Noncompliant Code Example

      namespace ns1 {
          extern "C" void doSomething();  // Noncompliant
          // ...
      }
      
      extern "C" {
        namespace ns2 {  // Noncompliant
          // ...
        }
        // ...
      }
      
      ns1::doSomething();
      doSomething(); // Works too, same as above
      

      Compliant Solution

      extern "C" void doSomething();
      
      namespace ns1 {
        // ...
      }
      
      extern "C" {
        // ...
      }
      
      namespace ns2 {  // Noncompliant
        // ...
      }
      
      doSomething();
      

      Exceptions

      extern "C" can prefix typedef.

      namespace ns1 {
          extern "C" typedef void c_function();  // Compliant, type named 'c_function' exists only in 'ns1' and not in the global namespace
          // ...
      }
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              jeanchristophe.collet Jean-Christophe Collet (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated: