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
11
votes
4 answers

Is RVO allowed when a copy constructor is private and not implemented?

Suppose I have a class where the copy constructor is private and not implemented (to make the object non-copyable) class NonCopyable { // whatever private: NonCopyable( const NonCopyable&); void operator=(const NonCopyable&); }; Now in…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
10
votes
2 answers

RVO with a standard layout struct without any constructors

I have a struct representing a binary message. I want to write a function to get the next such record from a buffer (whether a file or a socket, doesn't matter): template Record getNext(); Now, I could write this like: template…
Barry
  • 286,269
  • 29
  • 621
  • 977
10
votes
2 answers

Can a virtual function be a candidate to RVO (return value optimization)?

Are C++ compilers able to apply RVO for virtual functions? In this case: class AbstractReader { //... public: virtual std::vector getFloatVector() = 0; //... } class XmlReader : public AbstractReader { //... public: virtual…
galinette
  • 8,896
  • 2
  • 36
  • 87
10
votes
3 answers

g++: How RVO works in case that multiple translation units are involved

Firstly please take a look at the following code, which consists of 2 translation units. --- foo.h --- class Foo { public: Foo(); Foo(const Foo& rhs); void print() const; private: std::string str_; }; Foo getFoo(); --- foo.cpp…
Smg
  • 1,133
  • 1
  • 8
  • 9
9
votes
1 answer

How does C++ ABI deal with RVO and NRVO?

I am confused with regards how do compiler and linker deal with the fact that requirements on the caller of the function differ depending on if the function uses RVO or NRVO. This could be my misunderstanding, but my assumption is that generally…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
9
votes
2 answers

Timing of scope-based lock guards and return values

class C { mutable std::mutex _lock; map deep_member; public: auto get_big_lump() { std::unique_lock lock(_lock); // establish scope guard return deep_member; // copy the stuff while it can't be…
JDługosz
  • 5,592
  • 3
  • 24
  • 45
9
votes
1 answer

C++ universal reference in constructor and return value optimization (rvo)

Why does rvalue optimization not occur in classes with constructor with universal reference arguments? http://coliru.stacked-crooked.com/a/672f10c129fe29a0 #include template struct C { template
tower120
  • 5,007
  • 6
  • 40
  • 88
9
votes
4 answers

Will RVO happen when returning std::pair?

A function needs to return two values to the caller. What is the best way to implement? Option 1: pair myfunc() { ... return make_pair(getU(),getV()); } pair mypair = myfunc(); Option 1.1: // Same defn U u; V v; tie(u,v) =…
balki
  • 26,394
  • 30
  • 105
  • 151
9
votes
3 answers

When should I choose copy elision over passing argument by const reference?

Possible Duplicate: Is pass-by-value a reasonable default in C++11? I'm reading Want Speed? Pass by Value. by Dave Abrahams about copy elision and RVO. And I'm wondering why do we need the copy elision? I have been told too many times that you…
amazingjxq
  • 4,487
  • 7
  • 33
  • 35
8
votes
2 answers

When is RVO garanteed to apply / does apply with C++20 compilers

The C++ core guidelines states that F.20: For “out” output values, prefer return values to output parameters But then gives the following exception: struct Package { // exceptional case: expensive-to-move object char header[16]; …
Bérenger
  • 2,678
  • 2
  • 21
  • 42
8
votes
3 answers

Move constructor for returned objects breaks C++98 code?

The Standard doesn't not require a compiler to perform return-value-optimization(RVO), but then, since C++11, the result must be moved. It looks as if, this might introduce UB to/break code, which was valid in C++98. For example: #include…
ead
  • 32,758
  • 6
  • 90
  • 153
7
votes
1 answer

Does C++11 guarantee the local variable in a return statement will be moved rather than copied?

#include using namespace std; struct A { A(const vector&) {} A(vector&&) {} }; A f() { vector coll; return A{ coll }; // Which constructor of A will be called as per C++11? } int main() { f(); } Is…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
7
votes
2 answers

Is RVO applied on this situation?

Let's say we have this situation std::string v_1() { return "name"; } std::string test = v_1(); Is RVO applied here? I think that the answer is no, because one the rules to apply RVO is: "If a function returns a class type by value, and the…
user2296145
  • 276
  • 2
  • 8
7
votes
1 answer

Do RVO and copy elision only work within one compilation unit or not?

Do they work across different object files? Do they work across different DLLs? I know this depends on the compiler. I'm curious if there are any compilers and optimization settings that will make this work.
Sarien
  • 6,647
  • 6
  • 35
  • 55
7
votes
4 answers

How is moving a const returned object possible?

Lately, I have been reading this post and that post suggesting to stop returning const objects. This suggestion is also given by Stephan T. Lavavej in his talk in Going Native 2013. I wrote a very simple test to help me understand which…
Arnaud
  • 3,765
  • 3
  • 39
  • 69
1
2
3
9 10