Skip to content

Commit

Permalink
Improve reasoning and examples for F.48 (#2100)
Browse files Browse the repository at this point in the history
* fix incorrect reason for F.48

* distinguish rvo and nrvo

* the issue is about local, so limiting example to local

---------

Co-authored-by: Sergey Zubkov <[email protected]>
  • Loading branch information
Eisenwave and cubbimew authored Apr 11, 2024
1 parent 2a2581c commit c91ea43
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions CppCoreGuidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -3932,21 +3932,24 @@ value) of any assignment operator.

##### Reason

With guaranteed copy elision, it is now almost always a pessimization to expressly use `std::move` in a return statement.
Returning a local variable implicitly moves it anyway.
An explicit `std::move` is always a pessimization, because it prevents Return Value Optimization (RVO),
which can eliminate the move completely.

##### Example, bad

S f()
S bad()
{
S result;
return std::move(result);
}

##### Example, good

S f()
S good()
{
S result;
// Named RVO: move elision at best, move construction at worst
return result;
}

Expand Down

0 comments on commit c91ea43

Please sign in to comment.