mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2024-03-22 13:30:58 +08:00
R.34-35 shared_ptr examples (#1808)
This commit is contained in:
parent
ddc8f3e7d6
commit
375d452495
|
@ -9135,7 +9135,7 @@ Here, we ignore such cases.
|
|||
* [R.31: If you have non-`std` smart pointers, follow the basic pattern from `std`](#Rr-smart)
|
||||
* [R.32: Take a `unique_ptr<widget>` parameter to express that a function assumes ownership of a `widget`](#Rr-uniqueptrparam)
|
||||
* [R.33: Take a `unique_ptr<widget>&` parameter to express that a function reseats the `widget`](#Rr-reseat)
|
||||
* [R.34: Take a `shared_ptr<widget>` parameter to express that a function is part owner](#Rr-sharedptrparam-owner)
|
||||
* [R.34: Take a `shared_ptr<widget>` parameter to express shared ownership](#Rr-sharedptrparam-owner)
|
||||
* [R.35: Take a `shared_ptr<widget>&` parameter to express that a function might reseat the shared pointer](#Rr-sharedptrparam)
|
||||
* [R.36: Take a `const shared_ptr<widget>&` parameter to express that it might retain a reference count to the object ???](#Rr-sharedptrparam-const)
|
||||
* [R.37: Do not pass a pointer or reference obtained from an aliased smart pointer](#Rr-smartptrget)
|
||||
|
@ -9810,7 +9810,7 @@ Using `unique_ptr` in this way both documents and enforces the function call's r
|
|||
* (Simple) Warn if a function takes a `Unique_pointer<T>` parameter by lvalue reference and does not either assign to it or call `reset()` on it on at least one code path. Suggest taking a `T*` or `T&` instead.
|
||||
* (Simple) ((Foundation)) Warn if a function takes a `Unique_pointer<T>` parameter by reference to `const`. Suggest taking a `const T*` or `const T&` instead.
|
||||
|
||||
### <a name="Rr-sharedptrparam-owner"></a>R.34: Take a `shared_ptr<widget>` parameter to express that a function is part owner
|
||||
### <a name="Rr-sharedptrparam-owner"></a>R.34: Take a `shared_ptr<widget>` parameter to express shared ownership
|
||||
|
||||
##### Reason
|
||||
|
||||
|
@ -9818,11 +9818,16 @@ This makes the function's ownership sharing explicit.
|
|||
|
||||
##### Example, good
|
||||
|
||||
void share(shared_ptr<widget>); // share -- "will" retain refcount
|
||||
|
||||
void may_share(const shared_ptr<widget>&); // "might" retain refcount
|
||||
|
||||
void reseat(shared_ptr<widget>&); // "might" reseat ptr
|
||||
class WidgetUser
|
||||
{
|
||||
public:
|
||||
// WidgetUser will share ownership of the widget
|
||||
explicit WidgetUser(std::shared_ptr<widget> w) noexcept:
|
||||
m_widget{std::move(w)} {}
|
||||
// ...
|
||||
private:
|
||||
std::shared_ptr<widget> m_widget;
|
||||
};
|
||||
|
||||
##### Enforcement
|
||||
|
||||
|
@ -9842,11 +9847,11 @@ This makes the function's reseating explicit.
|
|||
|
||||
##### Example, good
|
||||
|
||||
void share(shared_ptr<widget>); // share -- "will" retain refcount
|
||||
|
||||
void reseat(shared_ptr<widget>&); // "might" reseat ptr
|
||||
|
||||
void may_share(const shared_ptr<widget>&); // "might" retain refcount
|
||||
void ChangeWidget(std::shared_ptr<widget>& w)
|
||||
{
|
||||
// This will change the callers widget
|
||||
w = std::make_shared<widget>(widget{});
|
||||
}
|
||||
|
||||
##### Enforcement
|
||||
|
||||
|
|
|
@ -646,6 +646,7 @@ webby
|
|||
Webcolor
|
||||
webcolors
|
||||
WG21
|
||||
WidgetUser
|
||||
WorkQueue
|
||||
'widen'
|
||||
x1
|
||||
|
|
Loading…
Reference in New Issue
Block a user