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

"shared_pointer" should not be taken by rvalue reference

    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:
      2min
    • Analysis Level:
      Semantic Analysis
    • Analysis Scope:
      Main Sources, Test Sources
    • CppCoreGuidelines:
      R.34

      Description

      Taking a shared_ptr by r-value reference is unnecessary. If done on purpose, it might imply that unique_ptr is a better choice since it transfers unique ownership.

      In general, a function should take:

      • A copy of a shared_ptr, if the function takes part in the ownership of the managed object
      • A reference to a shared_ptr if the function plans to modify the shared_ptr itself by calling reset or swap
      • A reference to a const shared_ptr, if the function might take part in the ownership by copying the reference to another shared_ptr on at least one of its paths
      • A raw pointer/reference to the object, if the function is only interested in the current value of the managed object

      Noncompliant Code Example

      Class Circle{};
      
      void fn(shared_ptr<Circle>&& circle);  // Noncompliant
      

      Compliant Solution

      Class Circle{};
      
      void fn(shared_ptr<Circle> circle); 
      

      Exceptions

      The move constructor of the shared_ptr itself.

      See

      • C++ Core Guidelines R.34 - Warn if a function takes a Shared_ptr<T> by rvalue reference. Suggesting taking it by value instead.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              abbas.sabra Abbas Sabra
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated: