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

avoiding code duplication for const-overload and RVO

For the sake of avoiding code duplication when dealing with const-overload I wrote something like this: #include class A { std::shared_ptr _data; public: const A lightCopy() const { A a; a._data = _data; …
Alexander Sergeyev
  • 922
  • 10
  • 19
0
votes
2 answers

Does RVO work with "new"?

Does copy elision kick in in this situation? In other words, do modern compilers with copy elision avoid to call any copy constructor here? class BigObj{}; BigObj fun() { BigObj b; return b; } int main() { BigObj *pb = new BigObj(fun()); } I…
Barney Szabolcs
  • 11,846
  • 12
  • 66
  • 91
0
votes
0 answers

Which factors determine whether (N)VRO is used?

(Inspired by this question which is unfortunately subjective). Which factors affect whether a compiler chooses to apply (N)VRO? I know there are two broad categories of factors as the optimizer needs to answer two questions about the (N)VRO…
MSalters
  • 173,980
  • 10
  • 155
  • 350
0
votes
1 answer

Why does the returned value need to be the first declared local for NRVO?

It is my understanding that for the compiler to be able to do Named Return Value Optimization (NRVO) the return value must be declared before any others in the function body. I suspect this may be due to the order of stack unwinding in the event of…
odinthenerd
  • 5,422
  • 1
  • 32
  • 61
0
votes
1 answer

Does passing a vector reference transfer ownership of its data?

What is the best way of passing and transferring ownership of a vector and it's data? In an ideal world, it would work something like this: std::vector& SpitAVector(int input) { std::vector result; result.push_back(input); …
Daniel
  • 678
  • 1
  • 5
  • 20
0
votes
1 answer

RVO Operators With Multiple Returns

We're working with RVO in class to show how we can reduce the number of temporaries created. I get the basics of it but I'm having difficulty understanding how to combine multiple values to return into one line. For single temp optimization I was…
0
votes
3 answers

Issue with return value optimisation

i was reading a bit about RVO in c++, and found a weird observation. I ran the below code.. class myClass { private: int *ptr; static int id; public: myClass() { id++; ptr = new int[10]; printf("Created %p id %d…
0
votes
2 answers

Returning a vector, is RVO or a move constructor being applied here?

I have a class, which has a std::vector data member. I then have a simple get member function which simply returns the data member by value. class X{ public: vector> z; vector> X::getVector(){ …
intrigued_66
  • 16,082
  • 51
  • 118
  • 189
0
votes
2 answers

Should I send std::set to function with refrence or not?

From another question I see this code : template std::set getUnion(const std::set& a, const std::set& b) { std::set result = a; result.insert(b.begin(), b.end()); return result; } Can't we just use below code ?…
uchar
  • 2,552
  • 4
  • 29
  • 50
0
votes
1 answer

Does RVO optimisation happens when a copy constructor is not defined for a class?

Lets say I have this piece of code: class Base { public: void f() {} private: int n; }; Base foo() { Base b; // processing return b; } Base doesn't have a copy constructor defined and looking at its members the compiler won't…
Adrian
  • 19,440
  • 34
  • 112
  • 219
0
votes
2 answers

Is there language level optimization like RVO and NRVO?

RVO is a compiler optimization but can provide a really useful performance boost. However it is not guaranteed and cannot be relied on. Is there anything in the language standard itself can optimize return value? Move semantics still copies the…
SwiftMango
  • 15,092
  • 13
  • 71
  • 136
0
votes
2 answers

RVO and NRVO optimisations + C++11 move operator

I am trying to figure out how RVO and NRVO work along with the new C++11 move operators. I have drafted a dummy class with a few examples. EDIT: Only most important part of the code is shown. Full source code is available here. I have two functions…
RicLeal
  • 923
  • 9
  • 23
0
votes
1 answer

C++ Return Value Optimization

I am new to c++, and I read a little bit on return value optimization on wiki and also this website, however I am still curious how the following behavior happens: using namespace std; class A { public: A() {cout << "A Ctor" <<…
QnA
  • 1,035
  • 10
  • 25
0
votes
2 answers

How can i avoid output parameters while doing backtracking?

As we know that output parameters are really bad thing void foo(set *x) Here x is actually expected parameters, this is pretty common practice in Linux system calls. But for c++ this seems not be a good coding practice. I have a scenario,…
innosam
  • 427
  • 1
  • 5
  • 18
0
votes
1 answer

Why RVO does not work with move constructor?

I am trying to test RVO and rvalue reference. Here is the code: #include using namespace std; class B{ public: int i; B(){ i = 0; cout << "B() " << i << endl; } B(const B& b){ i = 1; …
Zachary
  • 1,633
  • 2
  • 22
  • 34
1 2 3
9
10