From c91ea43aba3c02c75765404a3144ff6d5cd23640 Mon Sep 17 00:00:00 2001 From: Jan Schultke Date: Thu, 11 Apr 2024 18:59:42 +0200 Subject: [PATCH] Improve reasoning and examples for F.48 (#2100) * 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 --- CppCoreGuidelines.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 6de8e6e..038830a 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -3932,11 +3932,13 @@ 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); @@ -3944,9 +3946,10 @@ With guaranteed copy elision, it is now almost always a pessimization to express ##### Example, good - S f() + S good() { S result; + // Named RVO: move elision at best, move construction at worst return result; }