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 .

143 questions
220
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
  • 7,142
  • 12
  • 43
  • 85
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,926
  • 3
  • 34
  • 54
35
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
26
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
  • 5,486
  • 3
  • 30
  • 49
25
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
  • 3,186
  • 3
  • 28
  • 29
20
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
  • 15,686
  • 6
  • 47
  • 62
20
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
  • 1,034
  • 8
  • 19
15
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
  • 26,184
  • 12
  • 71
  • 97
14
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
  • 81,374
  • 13
  • 159
  • 204
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
  • 479
  • 3
  • 15
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
  • 28,205
  • 28
  • 128
  • 277
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,263
  • 11
  • 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: …
11
votes
0 answers

Does returning by const value affect return value optimization?

Consider the function const std::string f() { return "hello"; } And the call std::string x = f(); Regardless of whether value return types should be const or not, does the fact the return value is const, prevent a compiler from performing…
Neil Kirk
  • 21,327
  • 9
  • 53
  • 91
1
2 3
9 10