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

constexpr if and the return value optimization

I have this code: #include class A { public: // A(A const &) = delete; // Code fails if this is uncommented. explicit A(int); explicit A(::std::string const &); private: ::std::string myname_; int foo_; }; static…
Omnifarious
  • 54,333
  • 19
  • 131
  • 194
4
votes
2 answers

In c++, how to return multiple objects and nevertheless benefit from RVO

My function needs to return several large containers. To achieve this, the return statement creates a tuple consisting of the containers to return. However, based on my tests (using Apple clang version 11.0.0, clang-1100.0.33.17), copies are made,…
pnklein
  • 935
  • 6
  • 8
4
votes
1 answer

Will Go make a copy of my struct if I return by value, instead of a pointer?

Take this example: type Foo struct { num int } // func NewFoo() *Foo { // returning a pointer // return &Foo{33} // } func NewFoo() Foo { // NOT returning a pointer return Foo{33} } func main() { fff := NewFoo() …
rodrigocfd
  • 6,450
  • 6
  • 34
  • 68
4
votes
2 answers

C++ standard: return by copy to initialize a reference without RVO: is there any copy?

Let's consider the next sample: struct big_type {}; // Return by copy auto factory() { return big_type{}; } void any_scope_or_function() { big_type&& lifetime_extended = factory(); } Under the assumption RVO is forbidden or not present at all…
ABu
  • 10,423
  • 6
  • 52
  • 103
4
votes
3 answers

How implementing move constructor affects return value optimization?

Consider the following code snippet: #include #include class A { public: A() { std::cout << "A::A()\n"; } ~A() { std::cout << "A::~A()\n"; } A(const A&) = delete; A(A&&) { …
Anton K
  • 670
  • 6
  • 19
4
votes
1 answer

Does introducing a new variable defeat return value optimisation?

We all know that Foo returnAFoo() { return Foo(); } will be compiled with return value optimisation so a value copy will not be taken even if the copy constructor of Foo has side effects. But will Foo returnAFoo() { Foo f = Foo(); …
P45 Imminent
  • 8,319
  • 4
  • 35
  • 78
4
votes
1 answer

How does RVO and rvalue returned by the function work?

To understand the way how compiler selects constructor of a class, I wrote code following: #include struct Widget { Widget(Widget&& w){std::cout << "Move ctor" << std::endl;} Widget(void){std::cout << "Default ctor" <<…
darkspider
  • 136
  • 5
4
votes
0 answers

RVO and std::vector implementations

I wrote the code below to check that popular C++03 compilers implement RVO whenever possible. (See my related question about RVO Return value optimization: ho can I avoid copy construction of huge STL containers.). As far as I understand the short…
jimifiki
  • 5,377
  • 2
  • 34
  • 60
4
votes
1 answer

disable return-value-optimization for one function

struct X { void * a; void * b; }; X foo( void * u, void * v); foo() is implemented in assembler (i386) address of return value of type X is passed as hidden parameter to foo() if test code is compiled with -O0 the code works as expected if…
xlrg
  • 1,994
  • 1
  • 16
  • 14
4
votes
1 answer

C++11 tuple with copy elision or move semantic

I wrote a function like below: template std::tuple, T, T> f() { std::vector p(1000); return std::make_tuple(std::move(p), 10, 10); } Since the return type is quite complicated, is it guaranteed that under…
Huy Le
  • 416
  • 4
  • 6
4
votes
4 answers

Pointers to stack-allocated object and move-contruction

Note: This is a complete re-wording of a question I posted a while ago. If you find they are duplicate, please close the other one. My problem is quite general but it seems that it could be explained more easily based on a concrete simple example.…
Bérenger
  • 2,678
  • 2
  • 21
  • 42
4
votes
2 answers

Why do neither move semantics nor RVO work as expected?

I have recently stumbled upon some strange behaviour in my equation solver, which made me ask myself if I really understood how move semantics and RVO work together. There are plenty of related questions on this forum, and I've also read many…
Rene R.
  • 534
  • 1
  • 4
  • 12
4
votes
2 answers

Returning an fstream

I have this function: fstream open_user_file() const { ... } but my compiler complains about fstream copy-constructor being implicitly deleted. Given that the compiler performs RVO, why is the copy constructor chosen instead of the move…
qdii
  • 12,505
  • 10
  • 59
  • 116
4
votes
5 answers

Will C++ compiler optimize return-by-value code?

Lets assume i use Visual Studio or modern GCC with -O2. Will compiler create S inside func() and then copy it to a my_result, or will it create my_result with constructor (5, 6, 5 + 6) without creating temporary S? NOTE: Function func() definition…
pavelkolodin
  • 2,859
  • 3
  • 31
  • 74
3
votes
0 answers

std::move in sub-expressions of return statement

It's well-known that you shouldn't use std::move when returning a local variable: std::vector return_vector(void) { std::vector tmp {1,2,3,4,5}; return std::move(tmp); // DON'T DO THIS } But what about the case where your return…
MSalters
  • 173,980
  • 10
  • 155
  • 350