Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A20-8-6
#1
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-9...arameters/)?

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>());
}
Reply
#2
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.
Posted by and on behalf of
the MISRA C++ Working Group
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)