You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Modify R.34 and delete R.36 to conform to F.16-F.18
As it stands now, to my understanding R.34 conflicts with F.16-F.18.
R.34 suggests taking `shared_ptr` by value in a "sink" function,
but F.18 mentions explicitly that these sort of functions should pass
by `&&`, and specifically _not_ by value. This is mentioned by @hsutter in
[this comment](#1916 (comment))
So, this PR attempts to resolve the conflict by preferring the
"F.16-F.18" guidelines and attempting to modify the R guidelines to fit.
There are a number of other ways to avoid this ambiguity, for example
we could explicitly state that `shared_ptr` is not subject to
F.16-F.18 somewhere in those guidelines. R.34 could also be made to
fit if it were explicitly stated that `shared_ptr` be considered
"cheap to copy", although I don't see how R.36 could be made to fit
with F.16-F.18.
I don't have any strong opinion on what the "right" resolution is
here, but the fact that these two sets of guidelines seem to disagree
has been a source of confusion.
Copy file name to clipboardExpand all lines: CppCoreGuidelines.md
+22-30Lines changed: 22 additions & 30 deletions
Original file line number
Diff line number
Diff line change
@@ -2764,7 +2764,7 @@ Passing a shared smart pointer (e.g., `std::shared_ptr`) implies a run-time cost
2764
2764
void g(unique_ptr<int>);
2765
2765
2766
2766
// can only accept ints for which you are willing to share ownership
2767
-
void g(shared_ptr<int>);
2767
+
void g(const shared_ptr<int>&);
2768
2768
2769
2769
// doesn't change ownership, but requires a particular ownership of the caller
2770
2770
void h(const unique_ptr<int>&);
@@ -9297,9 +9297,8 @@ Here, we ignore such cases.
9297
9297
* [R.31: If you have non-`std` smart pointers, follow the basic pattern from `std`](#Rr-smart)
9298
9298
* [R.32: Take a `unique_ptr<widget>` parameter to express that a function assumes ownership of a `widget`](#Rr-uniqueptrparam)
9299
9299
* [R.33: Take a `unique_ptr<widget>&` parameter to express that a function reseats the `widget`](#Rr-reseat)
9300
-
* [R.34: Take a `shared_ptr<widget>` parameter to express shared ownership](#Rr-sharedptrparam-owner)
9300
+
* [R.34: Take a `shared_ptr<widget>&&` or `const shared_ptr<widget>&` parameter to express shared ownership](#Rr-sharedptrparam-owner)
9301
9301
* [R.35: Take a `shared_ptr<widget>&` parameter to express that a function might reseat the shared pointer](#Rr-sharedptrparam)
9302
-
* [R.36: Take a `const shared_ptr<widget>&` parameter to express that it might retain a reference count to the object ???](#Rr-sharedptrparam-const)
9303
9302
* [R.37: Do not pass a pointer or reference obtained from an aliased smart pointer](#Rr-smartptrget)
9304
9303
9305
9304
### <a name="Rr-raii"></a>R.1: Manage resources automatically using resource handles and RAII (Resource Acquisition Is Initialization)
@@ -9994,7 +9993,7 @@ Using `unique_ptr` in this way both documents and enforces the function call's r
9994
9993
* (Simple) Warn if a function takes a `Unique_pointer<T>` parameter by lvalue reference and does not either assign to it or call `reset()` on it on at least one code path. Suggest taking a `T*` or `T&` instead.
9995
9994
* (Simple) ((Foundation)) Warn if a function takes a `Unique_pointer<T>` parameter by reference to `const`. Suggest taking a `const T*` or `const T&` instead.
9996
9995
9997
-
### <a name="Rr-sharedptrparam-owner"></a>R.34: Take a `shared_ptr<widget>` parameter to express shared ownership
9996
+
### <a name="Rr-sharedptrparam-owner"></a>R.34: Take a `shared_ptr<widget>&&` or `const shared_ptr<widget>&` parameter to express shared ownership
9998
9997
9999
9998
##### Reason
10000
9999
@@ -10005,19 +10004,33 @@ This makes the function's ownership sharing explicit.
* (Simple) Warn if a function takes a `Shared_pointer<T>` parameter by lvalue reference and does not either assign to it or call `reset()` on it on at least one code path. Suggest taking a `T*` or `T&` instead.
10019
-
* (Simple) ((Foundation)) Warn if a function takes a `Shared_pointer<T>` by value or by reference to `const` and does not copy or move it to another `Shared_pointer` on at least one code path. Suggest taking a `T*` or `T&` instead.
10020
-
* (Simple) ((Foundation)) Warn if a function takes a `Shared_pointer<T>` by rvalue reference. Suggesting taking it by value instead.
10033
+
* (Simple) ((Foundation)) Warn if a function takes a `Shared_pointer<T>` by revalue reference or by reference to `const` and does not copy or move it to another `Shared_pointer` on at least one code path. Suggest taking a `T*` or `T&` instead.
10021
10034
10022
10035
### <a name="Rr-sharedptrparam"></a>R.35: Take a `shared_ptr<widget>&` parameter to express that a function might reseat the shared pointer
10023
10036
@@ -10040,28 +10053,7 @@ This makes the function's reseating explicit.
10040
10053
##### Enforcement
10041
10054
10042
10055
* (Simple) Warn if a function takes a `Shared_pointer<T>` parameter by lvalue reference and does not either assign to it or call `reset()` on it on at least one code path. Suggest taking a `T*` or `T&` instead.
10043
-
* (Simple) ((Foundation)) Warn if a function takes a `Shared_pointer<T>` by value or by reference to `const` and does not copy or move it to another `Shared_pointer` on at least one code path. Suggest taking a `T*` or `T&` instead.
10044
-
* (Simple) ((Foundation)) Warn if a function takes a `Shared_pointer<T>` by rvalue reference. Suggesting taking it by value instead.
10045
-
10046
-
### <a name="Rr-sharedptrparam-const"></a>R.36: Take a `const shared_ptr<widget>&` parameter to express that it might retain a reference count to the object ???
* (Simple) Warn if a function takes a `Shared_pointer<T>` parameter by lvalue reference and does not either assign to it or call `reset()` on it on at least one code path. Suggest taking a `T*` or `T&` instead.
10063
-
* (Simple) ((Foundation)) Warn if a function takes a `Shared_pointer<T>` by value or by reference to `const` and does not copy or move it to another `Shared_pointer` on at least one code path. Suggest taking a `T*` or `T&` instead.
10064
-
* (Simple) ((Foundation)) Warn if a function takes a `Shared_pointer<T>` by rvalue reference. Suggesting taking it by value instead.
10056
+
* (Simple) ((Foundation)) Warn if a function takes a `Shared_pointer<T>` by revalue reference or by reference to `const` and does not copy or move it to another `Shared_pointer` on at least one code path. Suggest taking a `T*` or `T&` instead.
10065
10057
10066
10058
### <a name="Rr-smartptrget"></a>R.37: Do not pass a pointer or reference obtained from an aliased smart pointer
0 commit comments