AUTOSAR C++14 Rule A20-8-4
A std::unique_ptr shall be used over std::shared_ptr if ownership sharing is not required
Since R2022b
Description
Rule Definition
A std::unique_ptr shall be used over std::shared_ptr if ownership sharing is not required.
Rationale
A resource can be owned by a single std::unique_ptr
object. In
contrast, a resource can be shared by several std::shared_ptr
objects.
Using std::unique_ptr
when resource sharing is not required has these advantages:
An
std::shared_ptr
object keeps an internal count of objects that share a resource. Copy assigning or copy constructing anstd::shared_ptr
object increments this use count. Anstd::unique_ptr
does not keep this count, making it a more efficient alternative.The resource owned by an
std::unique_ptr
object has a predictable life cycle. Unless its ownership is moved to a differentstd::unique_ptr
object, the resource is released when the object goes out of scope. The resource managed by anstd::shared_ptr
object has an unpredictable life cycle. It is not released until all the objects that share it are out of scope.
Using std::shared_ptr
objects makes your code inefficient
and difficult to debug. Avoid using std::shared_ptr
objects unless
ownership sharing is required. Instead, use std::unique_ptr
objects as
smart pointers.
Polyspace Implementation
Polyspace® raises a violation of this rule when replacing a
std::shared_ptr
object in your code by an
std::unique_ptr
object is possible. For instance, a violation is raised
when either of these conditions are true:
An
std::shared_ptr
is not used in a copy construction or copy assignment. The internal reference count remains at one. Consider this code:No#include <memory> class A(); void bar(std::shared_ptr<A> sp); void foo(){ auto sp = std::make_shared<A>();//count==1 bar(sp); }
std::shared_ptr
objects are copy constructed or copy assigned. The objectsp
does not share its resources. Declaring this object as anstd::unique_ptr
might be more efficient. See Avoid Using shared_ptr Objects If They are Not Copied.std::shared_ptr
objects are sequentially copied to create a chain ofN
shared pointers, but the first(N-1)
shared pointers are not dereferenced or passed to a function. Because these(N-1)
shared pointers are not used, moving their resources and declaring the pointers as unique pointers might be more efficient. For instance, in this code:A chain of shared pointers is created by copying#include <memory> class obj(); void foo(){ auto A = std::make_shared<obj>(); auto B = A;//count==2 auto C = B;//count==3 //… auto X = *C; }
A
intoB
and then copyingB
intoC
. BecauseA
andB
are not dereferenced, declaringA
,B
, andC
asstd::unique_ptr
objects does not result in loss of functionality and makes the code more efficient. See Avoid Using shared_ptr Objects When They Are Copied Once But Not Dereferenced.
As an exception, Polyspace does not raise a violation if you use an std::shared_ptr
object as an argument of certain standard atomic operation.
Troubleshooting
If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: General utilities library |
Category: Required, Automated |
Version History
Introduced in R2022b