From ee368e238e9daea6eb34bf8c82c06b43f806efbe Mon Sep 17 00:00:00 2001 From: bgloyer <36457894+bgloyer@users.noreply.github.com> Date: Thu, 1 Dec 2022 14:27:29 -0800 Subject: [PATCH] R.20: 'represent ownership' clean up example and enforcement (#1992) * Clean up R.20 example; enforcement * Clarified enforcement --- CppCoreGuidelines.md | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 8d5945f..1de6112 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -9788,19 +9788,17 @@ Consider: void f() { - X x; - X* p1 { new X }; // see also ??? - unique_ptr p2 { new X }; // unique ownership; see also ??? - shared_ptr p3 { new X }; // shared ownership; see also ??? - auto p4 = make_unique(); // unique_ownership, preferable to the explicit use "new" - auto p5 = make_shared(); // shared ownership, preferable to the explicit use "new" + X* p1 { new X }; // bad, p1 will leak + auto p2 = make_unique(); // good, unique ownership + auto p3 = make_shared(); // good, shared ownership } This will leak the object used to initialize `p1` (only). ##### 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. ### R.21: Prefer `unique_ptr` over `shared_ptr` unless you need to share ownership