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
**For an "output-only" value:** Prefer return values to output parameters.
2199
-
This includes large objects like standard containers that use implicit move operations for performance and to avoid explicit memory management.
2198
+
This includes large objects like standard containers that use implicit move operations for performance and to avoid explicit memory management. A return value is harder to miss and harder to misuse than a `T&` (an in-out parameter).
2200
2199
If you have multiple values to return, [use a tuple](#Rf-T-multi) or similar multi-member type.
2201
2200
2202
2201
##### Example
2203
2202
2204
-
vector<const int*> find_all(const vector<int>&, int x); // return pointers to elements with the value x
2203
+
vector<const int*> find_all(const vector<int>&, int x); // OK: return pointers to elements with the value x
2205
2204
2206
-
##### Example, bad
2205
+
void find_all(const vector<int>&, vector<const int*>& out, int x); // Bad: place pointers to elements with value x in out
2207
2206
2208
-
void find_all(const vector<int>&, vector<const int*>& out, int x); // place pointers to elements with value x in out
2207
+
##### Note
2208
+
2209
+
A struct of many (individually cheap-to-move) elements may be in aggregate expensive to move.
2209
2210
2210
2211
##### Exceptions
2211
2212
2212
2213
* For non-value types, such as types in an inheritance hierarchy, return the object by `unique_ptr` or `shared_ptr`.
2213
2214
* If a type is expensive to move (e.g., `array<BigPOD>`), consider allocating it on the free store and return a handle (e.g., `unique_ptr`), or passing it in a non-`const` reference to a target object to fill (to be used as an out-parameter).
2214
2215
* In the special case of allowing a caller to reuse an object that carries capacity (e.g., `std::string`, `std::vector`) across multiple calls to the function in an inner loop, treat it as an in/out parameter instead and pass by `&`. This is one use of the more generally named "caller-allocated out" pattern.
0 commit comments