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
6
votes
2 answers

Is it safe to modify RVO values within an RAII construct?

Consider the following program: #include #include class RvoObj { public: RvoObj(int x) : x_{x} {} RvoObj(const RvoObj& obj) : x_{obj.x_} { std::cout << "copied\n"; } RvoObj(RvoObj&& obj) : x_{obj.x_} {…
Brian Rodriguez
  • 4,250
  • 1
  • 16
  • 37
6
votes
2 answers

Why is "partial RVO" not performed?

Please take a look at this silly function, which should only illustrate the problem and a simplification of the real code: struct A; A create(bool first){ A f(21), s(42); if(first) return f; else return…
ead
  • 32,758
  • 6
  • 90
  • 153
6
votes
1 answer

How to ensure moving without impeding RVO?

When returning an object from a function one of the following cases can happen since C++11, assuming move- and copy-constructors are defined (see also the examples at the end of this post): it qualifies for copy-elision and compiler performs…
ead
  • 32,758
  • 6
  • 90
  • 153
6
votes
4 answers

Return value optimization: ho can I avoid copy construction of huge STL containers.

When I want a function to return me a container: vector func(){ vector result; ... return result; } To be used in the following way: vector result = func(); In order to avoid the overhead of copying my container I often…
jimifiki
  • 5,377
  • 2
  • 34
  • 60
6
votes
3 answers

C++ returning an object copy

I wrote the following code: class MyObjectHolder { public: std::vector getMyObject() const { return myObject; } private: std::vector myObject; }; At some point of my program I attempt to use the getMyObject method and…
Yippie-Ki-Yay
  • 22,026
  • 26
  • 90
  • 148
5
votes
6 answers

Return Value Optimization - C++ - Destructor calls

The following code calls the destructor 4 times: #include using namespace std; class A{ public: A(){cout<<"A"<
Venky
  • 71
  • 4
5
votes
2 answers

Why does Return Value Optimization not happen if no destructor is defined?

I expected to see copy elision from Named Return Value Optimization (NRVO) from this test program but its output is "Addresses do not match!" so NRVO didn't happen. Why is this? // test.cpp // Compile using: // g++ -Wall -std=c++17 -o test…
Ben C
  • 658
  • 6
  • 18
5
votes
3 answers

Can I rely on named return value optimisation for complicated return types?

Consider something like this: typedef std::unordered_multiset Set; typedef std::set SetOfSets; SetOfSets somethingRecursive(SomeType somethingToAnalyze) { Set s; // ... // check base cases, reduce somethingToAnalyze, fill in…
sigil
  • 815
  • 8
  • 16
5
votes
0 answers

Under what conditions do modern c++ compilers not perform rvo?

For performance reasons I'm interested in making sure that RVO is being performed on the return values of functions. What would prevent this optimization from being performed by a modern C++ compiler (such as gcc, clang, and msvc 2015) which I…
Mystor
  • 365
  • 3
  • 9
5
votes
1 answer

why doesn't c++ uses RVO when returning local std::stringstream?

I've read lots of info about rvalue and returning local variables in C++ >= 11. From what I understood is that "just return by value, do not use move/forward and do not add && to method signature and the compiler will optimize it for you". Okay, I…
Scott Tiger
  • 478
  • 2
  • 12
5
votes
1 answer

c++ std::move is bad here?

Let's suppose that I have struct Foo with move constructor and operator=(Foo&&), and I used it as data member: Foo f() { Foo foo; //code return foo; } struct Boo { Foo foo; Boo() { foo = f();//1 foo = std::move(f());//2 } }; In case (2)…
user1244932
  • 7,352
  • 5
  • 46
  • 103
5
votes
1 answer

Does Return Value Optimization need to declare a copy constructor

I have discovered that the Intel compiler does not generate return value optimization for std::array objects. The following code, which happen to be in the inner loop of my program is not optimized as it could. std::array f(const…
InsideLoop
  • 6,063
  • 2
  • 28
  • 55
5
votes
2 answers

return value optimization vs auto_ptr for large vectors

If I use auto_ptr as a return value of a function that populates large vectors, this makes the function a source function (it will create an internal auto_ptr and pass over ownership when it returns a non const auto_ptr). However, I cannot use this…
tmaric
  • 5,347
  • 4
  • 42
  • 75
5
votes
6 answers

Is the object copied or not when RVO/NRVO kicks in?

I can't get my head around RVO (and NRVO) definition because of multiple questions like this one that to me look assuming that RVO omits a copy constructor. Now according to 12.8.15 In such cases, the implementation treats the source and target of…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
4
votes
4 answers

Using a const reference to a returned by value value

Look at the following example: string foo(int i) { string a; ... Process i to build a ... return a; } void bar(int j) { const string& b = foo(j); cout << b; } I know RVO and NRVO, but I thought that in order to do that, I need to write…
brickner
  • 6,595
  • 3
  • 41
  • 54
1 2
3
9 10