RepeatingCallbacks: pass-by-value or const-ref when caller and callee use/copy and use it, respectively?

178 zobrazení
Preskočiť na prvú neprečítanú správu

Matthew Wolenetz

neprečítané,
14. 6. 2018, 17:39:2814. 6. 2018
komu: Chromium-dev
Inspecting https://chromium.googlesource.com/chromium/src/+/24b51059ba90de3650e81b784f798f1eff8e53bf/docs/callback.md#memory-management-and-passing, I'm a little confused.

Specifically, if callee keeps the RepeatingCallback parameter (or a copy of it), has ownership been transferred to callee (and pass-by-value should be used)? Or not? Even if the caller continues to use the RepeatingCallback object?

From chat on https://chromium-review.googlesource.com/c/chromium/src/+/1099935, it seems pass-by-value is best even in this case; if a copy of the (currently const&) param is taken by the callee, change to pass-by-value and consider callee using std::move to potentially reduce copy count back to the const& number -- all to improve readability and give caller flexibility to give up ownership independently of the callee.

Before I engage in related refactoring in //media (https://bugs.chromium.org/p/chromium/issues/detail?id=852972), I'd like to be sure I'm using the recommended style.

Thanks,
Matt

dan...@chromium.org

neprečítané,
14. 6. 2018, 17:52:4114. 6. 2018
komu: wolenetz, chromium-dev
On Thu, Jun 14, 2018 at 5:39 PM Matthew Wolenetz <wole...@chromium.org> wrote:
Inspecting https://chromium.googlesource.com/chromium/src/+/24b51059ba90de3650e81b784f798f1eff8e53bf/docs/callback.md#memory-management-and-passing, I'm a little confused.

Specifically, if callee keeps the RepeatingCallback parameter (or a copy of it), has ownership been transferred to callee (and pass-by-value should be used)? Or not? Even if the caller continues to use the RepeatingCallback object?

I believe you have it right. To reiterate: If the method wants to receive/take ownership, then it can receive it by value. If it just wants to use and not retain it, then it can receive it by reference.

Then, if the caller wants to keep ownership of a copy, it passes the callback as |foo|. And if it does not, and has the callback in an lvalue, it passes the callback as |std::move(foo)|.
 
From chat on https://chromium-review.googlesource.com/c/chromium/src/+/1099935, it seems pass-by-value is best even in this case; if a copy of the (currently const&) param is taken by the callee, change to pass-by-value and consider callee using std::move to potentially reduce copy count back to the const& number -- all to improve readability and give caller flexibility to give up ownership independently of the callee.

Before I engage in related refactoring in //media (https://bugs.chromium.org/p/chromium/issues/detail?id=852972), I'd like to be sure I'm using the recommended style.

Thanks,
Matt

Cheers,
Dana 

Matthew Wolenetz

neprečítané,
14. 6. 2018, 18:34:5314. 6. 2018
komu: Dana Jansens, chromium-dev
Thanks Dana - that helps :)

Matthew Wolenetz

neprečítané,
18. 6. 2018, 13:53:5118. 6. 2018
komu: Dana Jansens, chromium-dev
To help others who might have the same question, I've sent for review a CL to update //docs/callback.md: https://chromium-review.googlesource.com/c/chromium/src/+/1104882.
Odpovedať všetkým
Odpovedať autorovi
Poslať ďalej
0 nových správ