MISRA Discussion Forums
A20-8-6 - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C++ (https://forum.misra.org.uk/forumdisplay.php?fid=18)
+--- Forum: AUTOSAR C++:2014 rules (https://forum.misra.org.uk/forumdisplay.php?fid=185)
+--- Thread: A20-8-6 (/showthread.php?tid=1596)



A20-8-6 - martin.m.dowie - 12-01-2022

What is the preferred method of assigning to a local member data of type std::shared_ptr<T>?

Given the code below, are the lines indicated as A20-8-6 non-compliant really so and why? We can understand C2 (types match exactly). We can understand C4 (types match exactly and the member data is of std::shared_ptr<T>&.

The class C1 we believe is non-compliant but we can't justify ourselves of the reason why.

Is the rule trying to enforce 3 e) from GoTW #91 (https://herbsutter.com/2013/06/05/gotw-91-solution-smart-pointer-parameters/)?

Code:
struct S {};

class C1 {
    std::shared_ptr<S> s;
public:
    C1(const std::shared_ptr<S>& aS) : s(aS) {} // A20-8-6 non-compliant
};

class C2 {
    std::shared_ptr<S> s;
public:
    C2(std::shared_ptr<S> aS) : s(aS) {} // OK
};

class C3 {
    std::shared_ptr<S> s;
public:
    C3(std::shared_ptr<S>& aS) : s(aS) {} // A20-8-6 non-compliant
};

class C4 {
    const std::shared_ptr<S>& s;
public:
    C4(const std::shared_ptr<S>& aS) : s(aS) {} // OK
};

void f () {
    auto s = std::make_shared<S>();
    C1 c1(std::make_shared<S>());
    C2 c2(std::make_shared<S>());
    C3 c3(s);
    C4 c4(std::make_shared<S>());
}



RE: A20-8-6 - misra cpp - 26-01-2022

In the examples, you are not creating new owned objects. The owned objects were created earlier and are already owned by the parameters to these functions. Therefore, this rule does not apply to these examples.