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
0
votes
1 answer

What's better? Return a list of object pointers? or Return a pointer to list of objects?

I have a code that read Products from database and return them as a list. std::list findAll() {...} The code above (CRUD) is part of a library (third party library). Client code get the list and pass it over 3 layers (MVC). I know this…
jplc
  • 354
  • 1
  • 11
0
votes
1 answer

RVO on different compilers when using containers

I have the following code on c++17 template std::vector getPointerVector(std::vector base) { auto out = std::vector(); for (auto& t : base) { out.push_back(&t); } return out; } As far as i understand RVO should…
Burtan
  • 41
  • 4
0
votes
2 answers

Is a move constructor/assignment needed for RVO to kick in in C++11?

For example: In accepted answer https://stackoverflow.com/a/14623480/1423254, Does copy elision and RVO would still work for classes without move constructors? Yes, RVO still kicks in. Actually, the compiler is expected to pick: RVO (if…
Lukas Salich
  • 959
  • 2
  • 12
  • 30
0
votes
2 answers

Will (N)RVO be applied with my function in this situation?

I have the following code: (ok, in reality it's much more complicated, but I simplified it to make it easier to understand. so please disregard the things that seems stupid. I can't change them in my real situation) #include using…
conectionist
  • 2,694
  • 6
  • 28
  • 50
0
votes
2 answers

Assignment without copy in std move

I have a util function which returns a map std::map getFooMap() { std::map foo; // ... populate the map return foo; } From the caller side, I want to assign the map to a data field of some object. I can do: dest.data = getFooMap() Will this…
WhatABeautifulWorld
  • 3,198
  • 3
  • 22
  • 30
0
votes
1 answer

Compiler error : Only Copy elision is wanted, but move constructor seems to be required (by compiler)

Attempting to compile to this code give a compiler error with 'newer' compilers (im guessing: that support move constructors). Something along the lines of "attempting to call deleted function". It turns out that compiling with gcc 8.1 and clang…
darune
  • 10,480
  • 2
  • 24
  • 62
0
votes
1 answer

return rvalue of temporary as value

So, I have the following class: class Foo { public: Bar &&bar() { return std::move(_bar); } private: Bar _bar; } I know that it is valid to use this class in the following context: Bar bar; { Foo foo; bar = foo.bar(); //…
Felix
  • 6,885
  • 1
  • 29
  • 54
0
votes
1 answer

C++ copy on member access

I was doing some experiments to see when copy is performed apart from copy elision, RVO, NRVO cases. So I've written some code like this: class X { public: X() { std::cout << "Default constructor" << std::endl; } X(const X&) { std::cout <<…
0
votes
0 answers

Should I trust in RVO or use move semantic?

my question is how to write function which creates object. For example: class MyVector { public: std::vector vec_; MyVector() = default; MyVector(const MyVector&) = default; MyVector& operator=(const MyVector&) = default; …
user3191398
0
votes
1 answer

c++ Return Value Optization

I am hoping someone can shed some light on what RVO does in g++. I have some third party software that I need to modify, and I'd like to optimize it as best possible, but I'm having trouble figuring out what exactly RVO does, and when it kicks in.…
blackghost
  • 1,730
  • 11
  • 24
0
votes
0 answers

When can a compiler do Return value optimization

I came across rvo(Return value optimizatio) and to be honest, i have three questions about rvo. In which cases must a compiler do rvo (according to the standard complient)? In which cases is the compiler not allowed to do rvo (according to the…
user1235183
  • 3,002
  • 1
  • 27
  • 66
0
votes
3 answers

Is it a bad idea to return an object by value that contains a member vector?

Short version: If my object contains a std::vector, do the same rules of thumb apply to returning that object by value as to returning the vector by value? Does this change in C++11, which I understand "guarantees" returning a vector by value is…
Bear
  • 345
  • 4
  • 16
0
votes
2 answers

Compiler Optimization with return (std::stringstream ss).str()

The following function accepts a string as an argument, and returns another, after some processing. Is it fair enough to assume that the compiler will perform move optimizations, and I will not end up having the contents of string copied after…
hell_ical_vortex
  • 361
  • 2
  • 11
0
votes
1 answer

RVO in c++ and Query result as object or better pass by reference, if delegation is involved

I have to decide to return the result of a query with an object or pass into them by reference. The query will be at least one time delegated. Is there compiler RVO if I return an object and delegate it further. Here is an example: struct foo {int…
0
votes
1 answer

Does (N)RVO also happen when the value is being copied into an existent object?

(N)RVO helps to avoid unnecessary copying and creating of temporary objects when the return value is being assigned into a new variable (thus avoiding the copy constructor). So something like this should be optimized by RVO: MyObj getMyObj() { …
jbx
  • 21,365
  • 18
  • 90
  • 144