mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2024-03-22 13:30:58 +08:00
distinguish rvo and nrvo
This commit is contained in:
parent
e376433300
commit
8188d5e495
|
@ -3923,12 +3923,12 @@ value) of any assignment operator.
|
||||||
##### Reason
|
##### Reason
|
||||||
|
|
||||||
Returning a local variable implicitly moves it anyway.
|
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.
|
which can eliminate the move completely.
|
||||||
|
|
||||||
##### Example, bad
|
##### Example, bad
|
||||||
|
|
||||||
S f()
|
S bad()
|
||||||
{
|
{
|
||||||
S result;
|
S result;
|
||||||
return std::move(result);
|
return std::move(result);
|
||||||
|
@ -3936,9 +3936,13 @@ which can eliminate the move completely.
|
||||||
|
|
||||||
##### Example, good
|
##### Example, good
|
||||||
|
|
||||||
S f()
|
// RVO: guaranteed move elision when a temporary is returned
|
||||||
|
S rvo() { return S{}; }
|
||||||
|
|
||||||
|
S nrvo()
|
||||||
{
|
{
|
||||||
S result;
|
S result;
|
||||||
|
// Named RVO: move elision at best, move construction at worst
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user