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
1
vote
0 answers

VS2017: Copy Constructor doesn't work when move constructor is deleted

Consider the following code snippet: #include using namespace std; struct Snitch { Snitch() { cout << "c'tor" << endl; } ~Snitch() { cout << "d'tor" << endl; } Snitch(const Snitch&) { cout << "copy c'tor" << endl; } Snitch&…
David Hovsepyan
  • 509
  • 1
  • 3
  • 18
1
vote
2 answers

Is optimization applied to single-line functions?

I don't like to repeat myself in code, but also I don't want to lose performance by simple functions. Suppose the class has operator+ and function Add with same functionality (considering former as handy way of using class in expressions and latter…
Alexey S. Larionov
  • 6,555
  • 1
  • 18
  • 37
1
vote
1 answer

Will RVO work in this case?

I'm not sure if I should bother at all because it's not any safety critical application but I'm just curious and can't figure it out myself: will the compiler apply RVO for the following method? QJsonObject MyClass::buildObject(const QVector2D&…
tomdol
  • 381
  • 3
  • 14
1
vote
0 answers

How do i include RVO2 library in Visual Studio?

I have been writing a code which uses SDL to render particles in Visual Studio. However, there does not seem to be any way to include RVO2 library: http://gamma.cs.unc.edu/RVO2/downloads/ to Visual Studio. I have been able to include header files,…
1
vote
1 answer

std::move in return statements

I've been paying close attention to the advice never to write std::move in a return statement, for example. Except there are some edge cases, for example. I believe the following is another simple example of where std::move may be worthwhile - did I…
Rai
  • 1,328
  • 11
  • 20
1
vote
0 answers

Does (N)RVO apply to sub-objects (member or base) of identical size?

With RVO we can return a local variable from a function without incurring the cost of a copy. Does this also work when returning a sub-object of variable? From other answers I gather it does not when the complete object has a larger size than the…
Andre
  • 675
  • 4
  • 19
1
vote
1 answer

When RVO shows the maximum performance impact?

I've spent a little time trying to understand is RVO performance impact is as valuable as I thought. Here is my benchmark code (the main idea is to create big structures and return them from functions): #include #include #include…
Ivan Velichko
  • 6,348
  • 6
  • 44
  • 90
1
vote
4 answers

Can someone explain the output of this c++ program?

Why is the output foo3 equal to 3 ? i would suggest, when bar(foo1) is called, the function bar creates a copy of foo1 on the stack, so it's value is equal to 0, when this value is returned, the copy-constructor for foo3 increments the value again…
denelias
  • 21
  • 1
  • 7
1
vote
2 answers

Lambda expressions and RVO

Is the concept of "Return Value Optimization" applied for lambda expression in C++ Compilers? I know that it depends on the compiler and the optimization parameters but is it theoretical possible? BTW, does anyone know about this issue in VS.NET…
Humam Helfawi
  • 19,566
  • 15
  • 85
  • 160
1
vote
1 answer

What is the better way to use an error code in C++?

I'm a member of project which uses c++11. I'm not sure when I should use error code for return values. I found RVO in c++ works great even if string and struct data are returned directly. But if I use a return code, I cannot get the benefit of RVO…
jef
  • 3,890
  • 10
  • 42
  • 76
1
vote
1 answer

C++: RVO, NRVO and returning local objects

I just read about RVO (Return Value Optimization) and NRVO (Named Return Value Optimization). Below are two examples //Example of RVO Bar Foo() { return Bar(); } //Example of NVRO Bar Foo() { Bar bar; return bar; } That makes sense, a…
athos
  • 6,120
  • 5
  • 51
  • 95
1
vote
3 answers

How to force return value optimization in msvc

I have a function in a class that I want the compiler to use NRVO on...all the time...even in debug mode. Is there a pragma for this? Here is my class that works great in "release" mode: template class CBuffer { public: …
johnnycrash
  • 5,184
  • 5
  • 34
  • 58
0
votes
1 answer

Avoiding temporary variables when returning vectors from two or more functions

I wonder if there is any way to avoid extra copies when returning std::vector's by a function and "creating" their values directly in vector "integers". The most usual way of doing so before c++11 was passing main std::vector by reference (&) and…
Pablo
  • 557
  • 3
  • 16
0
votes
1 answer

Will structure like std::list> be copied when returned?

I have a function that is expected to extract a list of key values from a given string, where each key/value pair is represented by a std::pair, the function has the signature like below: std::list> extract(const string…
nybon
  • 8,894
  • 9
  • 59
  • 67
0
votes
1 answer

C++ return value and move rule exceptions

When we return a value from a C++ function copy-initialisation happens. Eg: std::string hello() { std::string x = "Hello world"; return x; // copy-init } Assume that RVO is disabled. As per copy-init rule if x is a non-POD class type, then…
tinkerbeast
  • 1,707
  • 1
  • 20
  • 42