AutosarC++18_10-A8.4.13

A std::shared_ptr shall be passed to a function as: (1) a copy to express the function shares ownership (2) an lvalue reference to express that the function replaces the managed object (3) a const lvalue reference to express that the function retains a reference count

Required inputs: IR

Rationale

Passing a std::shared_ptr by value (1) is clear and makes ownership sharing explicit.

Passing an lvalue reference (2) to std::shared_ptr is suggested to be used if a called function replaces the managed object on at least one code path, e.g. call assignment operator or reset method. Otherwise, it is recommended to pass an lvalue reference to the underlying object instead, see A8-4-11, A8-4-10.

Functions that take a const lvalue reference (3) to std::shared_ptr as a parameter are supposed to copy it to another std::shared_ptr on at least one code path, otherwise the parameter should be passed by a const lvalue reference to the underlying object instead, see A8-4-11, A8-4-10.

Example
// $Id: A8-4-13.cpp 308795 2018-02-23 09:27:03Z michal.szczepankiewicz $

    #include <memory>
    #include <iostream>

    //compliant, explicit ownership sharing
    void Value(std::shared_ptr<int> v) { }

    //compliant, replaces the managed object
    void Lv1(std::shared_ptr<int>& v)
    {
        v.reset();
    }

    //non-compliant, does not replace the managed object
    //shall be passed by int& so that API that does not
    //extend lifetime of an object is not polluted
    //with smart pointers
    void Lv2(std::shared_ptr<int>& v)
    {
        ++(*v);
    }

    //compliant, shared_ptr copied in the called function
    void Clv1(const std::shared_ptr<int>& v)
    {
        Value(v);
    }

    //non-compliant, const lvalue reference not copied
    //to a shared_ptr object on any code path
    //shall be passed by const int&
    void Clv2(const std::shared_ptr<int>& v)
    {
        std::cout << *v << std::endl;
    }

    //non-compliant
    void Rv1(std::shared_ptr<int>&& r) {}
See Also
  • C++ Core Guidelines [11]: R.34: Take a shared_ptr<widget> parameter to express that a function is part owner.
  • C++ Core Guidelines [11]: R.35: Take a shared_ptr<widget>& parameter to express that a function might reseat the shared pointer.
  • C++ Core Guidelines [11]: R.36: Take a const shared_ptr<widget>& parameter to express that it might retain a reference count to the object.

Possible Messages

Key

Text

Severity

Disabled

const_lvalue

std::shared_ptr passed as const lvalue reference but function does not copy it

None

False

lvalue

{} passed as lvalue reference but function does not replace object

None

False

shared_ptr_rvalue

std::shared_ptr passed as rvalue reference

None

False

unique_ptr_rvalue

std::unique_ptr passed as rvalue reference but function does not move object

None

False

Options

consider_shared_ptrs

consider_shared_ptrs : bool = True

Whether std::shared_ptrs should be considered
 

consider_unique_ptrs

consider_unique_ptrs : bool = False

Whether std::unique_ptrs should be considered