From 2cb21db559c57f2aa0c93d536f18417684adc4ba Mon Sep 17 00:00:00 2001 From: Herb Sutter Date: Thu, 18 Jan 2024 16:44:03 -0500 Subject: [PATCH] Add "take a copy for immediate local use" case to F.16 Closes #2170 --- CppCoreGuidelines.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 0a09b42a3..9291061bc 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -3007,7 +3007,8 @@ 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 keep a copy of the argument, in addition to passing by `const&` (for lvalues), +* If the function is going to keep a locally modifiable copy of the argument only for its own local use, taking it by value is fine +* If the function is going to keep a copy of the argument to pass to another destination (to another function, or store in a non-local location), 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). * In special cases, such as multiple "input + copy" parameters, consider using perfect forwarding. See [F.19](#Rf-forward).