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
Here, the writer of `g()` is supplying a buffer for `f()` to fill, but `f()` simply replaces it (at a somewhat higher cost than a simple copy of the characters).
2240
+
If the writer of `g()` makes an assumption about the size of `buffer` a bad logic error can happen.
2241
+
2242
+
2219
2243
**For an "input-only" value:** If the object is cheap to copy, pass by value; nothing beats the simplicity and safety of copying, and for small objects (up to two or three words) it is also faster than passing by reference.
2220
2244
Otherwise, pass by `const&` which is always cheap for larger objects. Both let the caller know that a function will not modify the argument, and both allow initialization by rvalues.
2221
2245
What is "cheap to copy" depends on the machine architecture, but two or three words (doubles, pointers, references) are usually best passed by value.
@@ -2280,6 +2304,7 @@ If you need the notion of an optional value, use a pointer, `std::optional`, or
2280
2304
* (Simple) ((Foundation)) Warn when a parameter being passed by value has a size greater than `4 * sizeof(int)`.
2281
2305
Suggest using a `const` reference instead.
2282
2306
* (Simple) ((Foundation)) Warn when a `const` parameter being passed by reference has a size less than `3 * sizeof(int)`. Suggest passing by value instead.
2307
+
* (Moderate) ((Foundation)) Warn about functions with non-`const` reference arguments that do *not* write to them.
2283
2308
2284
2309
2285
2310
**See also**: [implicit arguments](#Ri-explicit).
@@ -2418,40 +2443,6 @@ When I call `length(s)` should I test for `s == nullptr` first? Should the imple
2418
2443
**See also**: [Support library](#S-gsl).
2419
2444
2420
2445
2421
-
### <aname="Rf-T-ref"></a> F.22: Use a `T&` for an in-out-parameter
2422
-
2423
-
##### Reason
2424
-
2425
-
A called function can write to a non-`const` reference argument, so assume that it does.
2426
-
2427
-
##### Example
2428
-
2429
-
void update(Record& r); // assume that update writes to r
2430
-
2431
-
##### Note
2432
-
2433
-
A `T&` argument can pass information into a function as well as well as out of it.
2434
-
Thus `T&` could be and in-out-parameter. That can in itself be a problem and a source of errors:
Here, the writer of `g()` is supplying a buffer for `f()` to fill, but `f()` simply replaces it (at a somewhat higher cost than a simple copy of the characters).
2449
-
If the writer of `g()` makes an assumption about the size of `buffer` a bad logic error can happen.
2450
-
2451
-
##### Enforcement
2452
-
2453
-
* (Moderate) ((Foundation)) Warn about functions with non-`const` reference arguments that do *not* write to them.
2454
-
2455
2446
### <aname="Rf-T-return-out"></a> F.23: Use `T&` for an out-parameter that is expensive to move (only)
0 commit comments