From 94b567d2b8df72e5947ec11be0953d50c7c97514 Mon Sep 17 00:00:00 2001 From: hsutter Date: Mon, 25 Jan 2016 11:25:56 -0800 Subject: [PATCH] Updated F.7 and R.30 based on 2016-01-25 telecon discussion --- CppCoreGuidelines.md | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 701df32..df5a316 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -2208,6 +2208,18 @@ Passing a shared smart pointer (e.g., `std::shared_ptr`) implies a run-time cost void h(int&); // accepts any int +##### Example, bad + + // callee + void f(shared_ptr& w) + { + // ... + use(*w); // only use of w -- the lifetime is not used at all + // ... + }; + +See further in [R.30](#Rr-smartptrparam). + ##### Note We can catch dangling pointers statically, so we don't need to rely on resource management to avoid violations from dangling pointers. @@ -2218,7 +2230,7 @@ We can catch dangling pointers statically, so we don't need to rely on resource ##### Enforcement -* Difficult: Flag smart pointer parameters (parameters of a type that overloads `operator->` or `operator*`) that are never copied, moved from, or assigned to, or passed along to another function that could do so. That means the ownership semantics are not used. +* Flag a parameter of a smart pointer type (a type that overloads `operator->` or `operator*`) that is copyable but never copied/moved from in the function body or else movable but never moved from in the function body or by being a by-value parameter, and that is never assigned to, and that is not passed along to another function that could do so. That means the ownership semantics are not used. ### F.8: Prefer pure functions @@ -7302,7 +7314,8 @@ A function that does not manipulate lifetime should take raw pointers or referen ##### Enforcement -* (Simple) Warn if a function takes a parameter of a type that is a `unique_ptr` or `shared_ptr` and the function only calls any of: `operator*`, `operator->` or `get()`. +* (Simple) Warn if a function takes a parameter of a smart pointer type (that overloads `operator->` or `operator*`) `unique_ptr` or `shared_ptr` and the function only calls any of: `operator*`, `operator->` or `get()`. +* Flag a parameter of a smart pointer type (a type that overloads `operator->` or `operator*`) that is copyable but never copied/moved from in the function body or else movable but never moved from in the function body or by being a by-value parameter, and that is never assigned to, and that is not passed along to another function that could do so. That means the ownership semantics are not used. Suggest using a `T*` or `T&` instead. ### R.31: If you have non-`std` smart pointers, follow the basic pattern from `std`