From 8188d5e495547dc24e41db000247f143756c3436 Mon Sep 17 00:00:00 2001 From: Eisenwave Date: Sat, 24 Jun 2023 00:19:39 +0200 Subject: [PATCH] distinguish rvo and nrvo --- CppCoreGuidelines.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 6323adf..11f686e 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -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; }