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
There are a variety of ways to pass arguments to a function and to return values.
2185
+
There are a variety of ways to pass parameters to a function and to return values.
2187
2186
2188
2187
### <aname="Rf-conventional"></a> Rule F.15: Prefer simple and conventional ways of passing information
2189
2188
@@ -2314,12 +2313,24 @@ A reference may be assumed to refer to a valid object (language rule).
2314
2313
There is no (legitimate) "null reference."
2315
2314
If you need the notion of an optional value, use a pointer, `std::optional`, or a special value used to denote "no value."
2316
2315
2316
+
2317
+
**For an "forwarded" value:** If the object is to be passed onward to other code and not directly used by this function, we want to make this function agnostic to the argument `const`-ness and rvalue-ness. In that case, and only that case, make the parameter `TP&&` where `TP` is a template type parameter -- it both *ignores* and *preserves*`const`-ness and rvalue-ness. Therefore any code that uses a `T&&` is implicitly declaring that it itself doesn't care about the variable's `const`'-ness and rvalue-ness (because it is ignored), but that intends to pass the value onward to other code that does care about `const`-ness and rvalue-ness (because it is preserved). When used as a parameter `TP&&` is safe because any temporary objects passed from the caller will live for the duration of the function call. A parameter of type `TP&&` should essentially always be passed onward via `std::forward` in the body of the function.
2318
+
2319
+
##### Example
2320
+
2321
+
template <class F, class... Args>
2322
+
inline auto invoke(F&& f, Args&&... args) {
2323
+
return forward<F>(f)(forward<Args>(args)...);
2324
+
}
2325
+
2326
+
2317
2327
##### Enforcement
2318
2328
2319
2329
* (Simple) ((Foundation)) Warn when a parameter being passed by value has a size greater than `4 * sizeof(int)`.
2320
2330
Suggest using a `const` reference instead.
2321
2331
* (Simple) ((Foundation)) Warn when a `const` parameter being passed by reference has a size less than `3 * sizeof(int)`. Suggest passing by value instead.
2322
2332
* (Moderate) ((Foundation)) Warn about functions with non-`const` reference arguments that do *not* write to them.
2333
+
* Flag a function that takes a `TP&&` parameter (where `TP` is a template type parameter name) and uses it without `std::forward`.
2323
2334
2324
2335
2325
2336
**See also**: [implicit arguments](#Ri-explicit).
@@ -2458,23 +2469,6 @@ When I call `length(s)` should I test for `s == nullptr` first? Should the imple
2458
2469
**See also**: [Support library](#S-gsl).
2459
2470
2460
2471
2461
-
### <aname="Rf-pass-ref-ref"></a> F.24: Use a `TP&&` parameter when forwarding (only)
2462
-
2463
-
##### Reason
2464
-
2465
-
When `TP` is a template type parameter, `TP&&` is a forwarding reference -- it both *ignores* and *preserves*`const`'ness and rvalue-ness. Therefore any code that uses a `T&&` is implicitly declaring that it itself doesn't care about the variable's `const`'-ness and rvalue-ness (because it is ignored), but that intends to pass the value onward to other code that does care about `const`'-ness and rvalue-ness (because it is preserved). When used as a parameter `TP&&` is safe because any temporary objects passed from the caller will live for the duration of the function call. A parameter of type `TP&&` should essentially always be passed onward via `std::forward` in the body of the function.
2466
-
2467
-
##### Example
2468
-
2469
-
template <class F, class... Args>
2470
-
inline auto invoke(F&& f, Args&&... args) {
2471
-
return forward<F>(f)(forward<Args>(args)...);
2472
-
}
2473
-
2474
-
##### Enforcement
2475
-
2476
-
Flag a function that takes a `TP&&` parameter (where `TP` is a template type parameter name) and uses it without `std::forward`.
2477
-
2478
2472
### <aname="Rf-pass-ref-move"></a> F.25: Use a `T&&` parameter together with `move` for rare optimization opportunities
0 commit comments