Skip to content

Commit 622de22

Browse files
committed
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](isocpp#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.
1 parent fb112c7 commit 622de22

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

CppCoreGuidelines.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2978,6 +2978,11 @@ Use the advanced techniques only after demonstrating need, and document that nee
29782978

29792979
For passing sequences of characters see [String](#SS-string).
29802980

2981+
##### Exception
2982+
2983+
To express shared ownership using `shared_ptr` types, rather than following guidelines F.16-21,
2984+
instead follow [R.34](#Rr-sharedptrparam-owner), [R.35](#Rr-sharedptrparam), and [R.36](#Rr-sharedptrparam-const).
2985+
29812986
### <a name="Rf-in"></a>F.16: For "in" parameters, pass cheaply-copied types by value and others by reference to `const`
29822987

29832988
##### Reason
@@ -3029,6 +3034,11 @@ If you need the notion of an optional value, use a pointer, `std::optional`, or
30293034
* (Simple) ((Foundation)) Warn when a parameter passed by reference to `const` has a size less than `2 * sizeof(void*)`. Suggest passing by value instead.
30303035
* (Simple) ((Foundation)) Warn when a parameter passed by reference to `const` is `move`d.
30313036

3037+
##### Exception
3038+
3039+
To express shared ownership using `shared_ptr` types, instead follow [R.34](#Rr-sharedptrparam-owner) or [R.36](#Rr-sharedptrparam-const),
3040+
depending on whether or not the function unconditionally takes a reference to the argument.
3041+
30323042
### <a name="Rf-inout"></a>F.17: For "in-out" parameters, pass by reference to non-`const`
30333043

30343044
##### Reason
@@ -3107,6 +3117,10 @@ For example:
31073117
// use p ... possibly std::move(p) onward somewhere else
31083118
} // p gets destroyed
31093119

3120+
##### Exception
3121+
3122+
If the "will-move-from" parameter is a `shared_ptr` instead follow [R.34](#Rr-sharedptrparam-owner) and pass the `shared_ptr` by value.
3123+
31103124
##### Enforcement
31113125

31123126
* Flag all `X&&` parameters (where `X` is not a template type parameter name) where the function body uses them without `std::move`.

0 commit comments

Comments
 (0)