-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Explain freedoms available in F.16-F.18 #1412
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2825,6 +2825,8 @@ Advanced parameter passing: | |
|  | ||
|
|
||
| Use the advanced techniques only after demonstrating need, and document that need in a comment. | ||
| Note that the guidelines do not provide a complete decision procedure on how to write a function. | ||
| In many situations, there will be multiple ways to write a function that is in accordance with the guidelines. | ||
|
|
||
| ### <a name="Rf-in"></a>F.16: For "in" parameters, pass cheaply-copied types by value and others by reference to `const` | ||
|
|
||
|
|
@@ -2847,9 +2849,9 @@ When copying is cheap, nothing beats the simplicity and safety of copying, and f | |
|
|
||
| For advanced uses (only), where you really need to optimize for rvalues passed to "input-only" parameters: | ||
|
|
||
| * If the function is going to unconditionally move from the argument, take it by `&&`. See [F.18](#Rf-consume). | ||
| * If the function is going to unconditionally move from the argument, take it by `&&`. See [F.18](#Rf-consume). You may also provide a `const &` overload that copies the argument rather than moves it, although this is not required. | ||
| * If the function is going to keep a copy of the argument, in addition to passing by `const&` (for lvalues), | ||
| add an overload that passes the parameter by `&&` (for rvalues) and in the body `std::move`s it to its destination. Essentially this overloads a "will-move-from"; see [F.18](#Rf-consume). | ||
| add an overload that passes the parameter by `&&` (for rvalues) and in the body `std::move`s it to its destination. Essentially this overloads a "will-move-from"; see [F.18](#Rf-consume). It also could be appropriate to remove the `const&` overload and only keep the `&&` overload. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why would it be appropriate to remove the |
||
| * In special cases, such as multiple "input + copy" parameters, consider using perfect forwarding. See [F.19](#Rf-forward). | ||
|
|
||
| ##### Example | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is under the text For [...] where you really need to optimize for rvalues [...] I think this change is a little bit redundant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This came from a specific point I was confused by, which is that the guidelines allow you to provide only an
&&overload in these situations. To me, this is a useful clarification since it explicitly says that you're not required to provide aconst &overload as well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on the
shared_ptrguidance in R.34 (and the discussion in #1989), should F.18 be updated to allow pass by value and then move?or
Pass by value is simpler than
const&and&&overloads but does result in an extra move. I sometimes pass vectors this way in my own code. F.18 already has an exception forunique_ptr, should another exception be added for cheap to move types?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those are two different cases: A&& forces a r- value to be passed (this is the classic "consume" semantic. A is a simplified version of the
const A&andA&&overload set, which means I will keep a copy.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I cant move from a const&, so this change seems wrong to me.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What this change meant to convey is - If you wrote a move-only sink function, it must use
&&. However, it may be more convenient for the caller if you provide both a&&andconst &overload, and this is indeed also allowed by the guidelines. As written I've seen many people be confused by this note, since they thought since they wrote a function that always moves an argument, they are somehow not allowed to provide aconst &overload. They probably got this impression because they only read this note and did not perform a close reading of the full set of guidelinesF.16-F.18. That's the confusion I'm trying to avoid by editing this note. Could you suggest a better way to make this clarification?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regarding @bgloyer 's suggestion to change the guidelines to allow pass-by-value, I'm in support of this but this would be more disruptive than the change I was trying to make on this PR. In particular, I'm a big fan of the alternative function passing guidelines proposed here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess the first question is should the F.call guidelines be updated to allow pass by value and move if the callee consumes the parameter? Then if so, where should it go?