-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Why Resource-Management is no discussion about shared_ptr<widget>&& and unique_ptr<widget>&& ? #1916
Comments
This was mentioned in Issue #1781 for shared_ptr |
Editors call: F.18 covers that for "sink" functions (for smart pointers, ownership transfer) to pass by |
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](isocpp#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.
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](isocpp#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.
Should add the discussion about
shared_ptr<widget>&&
andunique_ptr<widget>&&
in Smart pointer rule summary .Link: Smart pointer rule summary
For R.34's example code, when enter the function, the shared_ptr is copy-constructed, and this requires incrementing the strong reference count. This can incur performance costs. But if the parameter type is
shared_ptr<widget>&&
, this directly addresses the cost. The rvalue reference typeshared_ptr<widget>&&
is more efficient than the value typeshared_ptr<widget>
. So, I don't understand R.34 Enforcement's final line:I modified the example code for R.34 , I think the example code should be written like this:
On the other hand, R.32 and R.33 talk about
unique_ptr<widget>
andunique_ptr<widget>&
. But I think,unique_ptr<widget>&&
is also very useful. If we take aunique_ptr<widget>
parameter, its total cost would be two moves. But if we take aunique_ptr<widget>&&
parameter, the total cost is one move.The screenshots from the Scott Meyers's Effective Modern C++ Item41 is as follows:
So, I think we should add the discussion about
shared_ptr<widget>&&
andunique_ptr<widget>&&
in Smart pointer rule summary .The text was updated successfully, but these errors were encountered: