mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2025-12-17 12:44:42 +03:00
R.20: 'represent ownership' clean up example and enforcement (#1992)
* Clean up R.20 example; enforcement * Clarified enforcement
This commit is contained in:
@@ -9788,19 +9788,17 @@ Consider:
|
|||||||
|
|
||||||
void f()
|
void f()
|
||||||
{
|
{
|
||||||
X x;
|
X* p1 { new X }; // bad, p1 will leak
|
||||||
X* p1 { new X }; // see also ???
|
auto p2 = make_unique<X>(); // good, unique ownership
|
||||||
unique_ptr<X> p2 { new X }; // unique ownership; see also ???
|
auto p3 = make_shared<X>(); // good, shared ownership
|
||||||
shared_ptr<X> p3 { new X }; // shared ownership; see also ???
|
|
||||||
auto p4 = make_unique<X>(); // unique_ownership, preferable to the explicit use "new"
|
|
||||||
auto p5 = make_shared<X>(); // shared ownership, preferable to the explicit use "new"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
This will leak the object used to initialize `p1` (only).
|
This will leak the object used to initialize `p1` (only).
|
||||||
|
|
||||||
##### Enforcement
|
##### Enforcement
|
||||||
|
|
||||||
(Simple) Warn if the return value of `new` or a function call with return value of pointer type is assigned to a raw pointer.
|
* (Simple) Warn if the return value of `new` is assigned to a raw pointer.
|
||||||
|
* (Simple) Warn if the result of a function returning a raw owning pointer is assigned to a raw pointer.
|
||||||
|
|
||||||
### <a name="Rr-unique"></a>R.21: Prefer `unique_ptr` over `shared_ptr` unless you need to share ownership
|
### <a name="Rr-unique"></a>R.21: Prefer `unique_ptr` over `shared_ptr` unless you need to share ownership
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user