Skip to content

Commit

Permalink
Add exceptions to F.15, F.16, and F.18 for shared_ptr types (#2010)
Browse files Browse the repository at this point in the history
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](#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.
  • Loading branch information
russellmcc authored Jan 19, 2023
1 parent fa31c19 commit 1ba3371
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions CppCoreGuidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

### <a name="Rf-in"></a>F.16: For "in" parameters, pass cheaply-copied types by value and others by reference to `const`

##### Reason
Expand Down Expand Up @@ -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 or equal 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.

### <a name="Rf-inout"></a>F.17: For "in-out" parameters, pass by reference to non-`const`

##### Reason
Expand Down Expand Up @@ -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`.
Expand Down

0 comments on commit 1ba3371

Please sign in to comment.