distinguish rvo and nrvo

This commit is contained in:
Eisenwave 2023-06-24 00:19:39 +02:00 committed by GitHub
parent e376433300
commit 8188d5e495
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3923,12 +3923,12 @@ value) of any assignment operator.
##### Reason
Returning a local variable implicitly moves it anyway.
An explicit `std::move` is also a pessimization, because it prevents Named Return Value Optimization (NRVO),
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);
@ -3936,9 +3936,13 @@ which can eliminate the move completely.
##### Example, good
S f()
// RVO: guaranteed move elision when a temporary is returned
S rvo() { return S{}; }
S nrvo()
{
S result;
// Named RVO: move elision at best, move construction at worst
return result;
}