Questions tagged [rvo]

C++ copy-elision of return-values

(Named) Return Value Optimization is an exception to the as-if rule governing optimizations C++ implementations may perform.

It allows elliding a copy of the (named) return-value, if it is a local variable or a temporary.

See .

146 questions
190
votes
4 answers

c++11 Return value optimization or move?

I don't understand when I should use std::move and when I should let the compiler optimize... for example: using SerialBuffer = vector< unsigned char >; // let compiler optimize it SerialBuffer read( size_t size ) const { SerialBuffer buffer(…
Elvis Dukaj
  • 6,441
  • 10
  • 38
  • 77
58
votes
2 answers

Why does std::move prevent RVO?

In many cases when returning a local from a function, RVO kicks in. However, I thought that explicitly using std::move would at least enforce moving when RVO does not happen, but that RVO is still applied when possible. However, it seems that this…
cdoubleplusgood
  • 1,199
  • 1
  • 9
  • 11
42
votes
1 answer

Why does Visual Studio not perform return value optimization (RVO) in this case

I was answering a question and recommending return by-value for a large type because I was confident the compiler would perform return-value optimization (RVO). But then it was pointed out to me that Visual Studio 2013 was not performing RVO on my…
Chris Drew
  • 14,004
  • 3
  • 30
  • 51
32
votes
1 answer

C++11 move when returning a lock

In the book "C++ Concurrency in Action" reading the following method std::unique_lock wait_for_data() { std::unique_lock head_lock(head_mutex); data_cond.wait(head_lock,[&]{return head.get()!=get_tail();}); return…
user2026095
23
votes
2 answers

Why is RVO disallowed when returning a parameter?

It's stated in [C++11: 12.8/31] : This elision of copy/move operations, called copy elision, is permitted [...] : — in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object…
Valentin Milea
  • 2,905
  • 2
  • 24
  • 26
19
votes
3 answers

C++ Unified Assignment Operator move-semantics

EDIT: solved see comments --don't know how to mark as solved with out an answer. After watching a Channel 9 video on Perfect Forwarding / Move semantics in c++0x i was some what led into believing this was a good way to write the new assignment…
111111
  • 14,528
  • 6
  • 38
  • 58
19
votes
1 answer

Why Structured Bindings disable both RVO and move on return statement?

Suppose we have a class named AAA that supports both copy/move: class AAA { public: AAA() = default; ~AAA() = default; AAA(const AAA& rhs) { std::cout << "Copy constructor" << std::endl; } AAA(AAA&& rhs) { …
Dean Seo
  • 4,842
  • 3
  • 22
  • 47
19
votes
4 answers

Return value optimization of tuple/tie

I am looking into return value optimization in the case of tuple/ties and the behavior I observe is not as I expected. In the example below I would expect move semantics to kick in, which it does, but there is one copy operation which remains. The…
thorsan
  • 994
  • 7
  • 19
14
votes
2 answers

Multiple return values (structured bindings) with unmovable types and guaranteed RVO in C++17

With C++ 17, we will have the possibility to return unmovable (including uncopyable) types such as std::mutex, via what can be thought of as guaranteed return value optimization (RVO): Guaranteed copy elision through simplified value categories:…
Johan Lundberg
  • 23,281
  • 9
  • 67
  • 91
13
votes
1 answer

Why are the RVO requirements so restrictive?

Yet another "why must std::move prevent the (unnamed) return-value-optimization?" question: Why does std::move prevent RVO? explains that the standard specifically requires that the function's declared return type must match the type of the…
jamesdlin
  • 48,496
  • 10
  • 105
  • 134
13
votes
2 answers

Returning member unique_ptr from class method

I am trying to return a std::unique_ptr class member (trying to move the ownership) to the caller. The following is a sample code snippet: class A { public: A() : p {new int{10}} {} static std::unique_ptr Foo(A &a) { return a.p; //…
axg
  • 133
  • 4
12
votes
1 answer

Expensive to move types

I am reading the official CPPCoreGuidelines to understand correctly when it's reliable to count on RVO and when not. At F20 it is written: If a type is expensive to move (e.g., array), consider allocating it on the free store and return a handle…
Taw
  • 386
  • 2
  • 12
11
votes
2 answers

Why do I not get guaranteed copy elision with std::tuple?

I would expect that in C++20 the following code prints nothing between prints of A and B (since I expect guaranteed RVO to kick in). But output is: A Bye B C Bye Bye So presumably one temporary is being created. #include #include…
NoSenseEtAl
  • 23,776
  • 22
  • 102
  • 222
11
votes
1 answer

C++ return value optimization, multiple unnamed returns

Let's consider these two functions : // 1. Multiple returns of the same named object string f() { string s; if (something()) return s.assign(get_value1()); else return s.assign(get_value2()); } and // 2. Multiple…
n.caillou
  • 1,090
  • 8
  • 15
11
votes
1 answer

why C++ destuctor affect the behavior of return value optimization

I have simplified my code as follows. #include class NoncopyableItem { public: NoncopyableItem() { } NoncopyableItem(NoncopyableItem &&nt) { }; }; class Iterator { friend class Factory; public: ~Iterator() { } // weird private: …
1
2 3
9 10