From 15cb3e1911a8e8a6c357323f8964ad0b07c56013 Mon Sep 17 00:00:00 2001 From: Russell McClellan Date: Thu, 15 Dec 2022 17:04:10 -0500 Subject: [PATCH] Add exceptions to F.16, F.16, and F.18 for `shared_ptr` types Currently these guidelines conflict with R.34, R.35, and R.36. This conflict has led to confusion, where it's unclear which guidelines to prefer for `shared_ptr` types. In a [previous PR](https://github.com/isocpp/CppCoreGuidelines/pull/1989) I proposed preferring the "F" series of guidelines. This PR takes the opposite approach and prefers the "R" guidelines for `shared_ptr` types. I don't feel strongly about which guidelines to prefer, I just want to make sure the guidelines are internally consistent. --- CppCoreGuidelines.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 4d8297cb2..f35b69818 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -2978,6 +2978,11 @@ Use the advanced techniques only after demonstrating need, and document that nee For passing sequences of characters see [String](#SS-string). +##### Exception + +To express shared ownership using `shared_ptr` types, rather than following guidelines F.16-21, +instead follow [R.34](#Rr-sharedptrparam-owner), [R.35](#Rr-sharedptrparam), and [R.36](#Rr-sharedptrparam-const). + ### F.16: For "in" parameters, pass cheaply-copied types by value and others by reference to `const` ##### Reason @@ -3029,6 +3034,11 @@ If you need the notion of an optional value, use a pointer, `std::optional`, or * (Simple) ((Foundation)) Warn when a parameter passed by reference to `const` has a size less than `2 * sizeof(void*)`. Suggest passing by value instead. * (Simple) ((Foundation)) Warn when a parameter passed by reference to `const` is `move`d. +##### Exception + +To express shared ownership using `shared_ptr` types, instead follow [R.34](#Rr-sharedptrparam-owner) or [R.36](#Rr-sharedptrparam-const), +depending on whether or not the function unconditionally takes a reference to the argument. + ### F.17: For "in-out" parameters, pass by reference to non-`const` ##### Reason @@ -3107,6 +3117,10 @@ For example: // use p ... possibly std::move(p) onward somewhere else } // p gets destroyed +##### Exception + +If the "will-move-from" parameter is a `shared_ptr` instead follow [R.34](#Rr-sharedptrparam-owner) and pass the `shared_ptr` by value. + ##### Enforcement * Flag all `X&&` parameters (where `X` is not a template type parameter name) where the function body uses them without `std::move`.