Skip to content

Commit a534f4a

Browse files
authored
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.
1 parent ddef6cd commit a534f4a

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

CppCoreGuidelines.md

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2927,31 +2927,11 @@ For advanced uses (only), where you really need to optimize for rvalues passed t
29272927

29282928
void sink(unique_ptr<widget>); // input only, and moves ownership of the widget
29292929

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)).
29502932

29512933
##### Notes
29522934

2953-
The return value optimization doesn't handle the assignment case, but the move assignment does.
2954-
29552935
A reference can be assumed to refer to a valid object (language rule).
29562936
There is no (legitimate) "null reference."
29572937
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.
31043084
* 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).
31053085
* 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).
31063086

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.
3106+
31073107
##### Example
31083108

31093109
struct Package { // exceptional case: expensive-to-move object

0 commit comments

Comments
 (0)