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
F.16 ("in" parameters): Move Matrix example to F.20 (return values) (#1922)
The `Matrix` example and the notes about assignment appear off-topic in rule F.16, as F.16 is specifically about "in" parameters.
With help from Sergey Zubkov.
Copy file name to clipboardExpand all lines: CppCoreGuidelines.md
+22-22Lines changed: 22 additions & 22 deletions
Original file line number
Diff line number
Diff line change
@@ -2927,31 +2927,11 @@ For advanced uses (only), where you really need to optimize for rvalues passed t
2927
2927
2928
2928
void sink(unique_ptr<widget>); // input only, and moves ownership of the widget
2929
2929
2930
-
Avoid "esoteric techniques" such as:
2931
-
2932
-
* Passing arguments as `T&&` "for efficiency".
2933
-
Most rumors about performance advantages from passing by `&&` are false or brittle (but see [F.18](#Rf-consume) and [F.19](#Rf-forward)).
2934
-
* Returning `const T&` from assignments and similar operations (see [F.47](#Rf-assignment-op).)
2935
-
2936
-
##### Example
2937
-
2938
-
Assuming that `Matrix` has move operations (possibly by keeping its elements in a `std::vector`):
2939
-
2940
-
Matrix operator+(const Matrix& a, const Matrix& b)
2941
-
{
2942
-
Matrix res;
2943
-
// ... fill res with the sum ...
2944
-
return res;
2945
-
}
2946
-
2947
-
Matrix x = m1 + m2; // move constructor
2948
-
2949
-
y = m3 + m3; // move assignment
2930
+
Avoid "esoteric techniques" such as passing arguments as `T&&` "for efficiency".
2931
+
Most rumors about performance advantages from passing by `&&` are false or brittle (but see [F.18](#Rf-consume) and [F.19](#Rf-forward)).
2950
2932
2951
2933
##### Notes
2952
2934
2953
-
The return value optimization doesn't handle the assignment case, but the move assignment does.
2954
-
2955
2935
A reference can be assumed to refer to a valid object (language rule).
2956
2936
There is no (legitimate) "null reference."
2957
2937
If you need the notion of an optional value, use a pointer, `std::optional`, or a special value used to denote "no value."
@@ -3104,6 +3084,26 @@ The argument against is that it prevents (very frequent) use of move semantics.
3104
3084
* 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 reference to non-`const` target object to fill (to be used as an out-parameter).
3105
3085
* 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 and pass by reference](#Rf-out-multi).
3106
3086
3087
+
##### Example
3088
+
3089
+
Assuming that `Matrix` has move operations (possibly by keeping its elements in a `std::vector`):
3090
+
3091
+
Matrix operator+(const Matrix& a, const Matrix& b)
3092
+
{
3093
+
Matrix res;
3094
+
// ... fill res with the sum ...
3095
+
return res;
3096
+
}
3097
+
3098
+
Matrix x = m1 + m2; // move constructor
3099
+
3100
+
y = m3 + m3; // move assignment
3101
+
3102
+
3103
+
##### Note
3104
+
3105
+
The return value optimization doesn't handle the assignment case, but the move assignment does.
0 commit comments