简体   繁体   中英

C++ understanding RVO (as compared to returning local variable reference)

It's my first year of using C++ and learning on the way. I'm currently reading up on Return Value Optimizations (I use C++11 btw). Eg here https://en.wikipedia.org/wiki/Return_value_optimization , and immediately these beginner examples with primitive types spring to mind:

int& func1()
{
    int i = 1;
    return i;
} 
//error, 'i' was declared with automatic storage (in practice on the stack(?)) 
//and is undefined by the time function returns 

...and this one:

int func1()
{
    int i = 1;
    return i;
}
//perfectly fine, 'i' is copied... (to previous stack frame... right?)

Now, I get to this and try to understand it in the light of the other two:

Simpleclass func1()
{
    return Simpleclass();
}

What actually happens here? I know most compilers will optimise this, what I am asking is not 'if' but:

  • how the optimisation works (the accepted response)
  • does it interfere with storage duration: stack/heap ( Old: Is it basically random whether I've copied from stack or created on heap and moved (passed the reference)? Does it depend on created object size? )
  • is it not better to use, say, explicit std::move ?

You won't see any effect of RVO when returning ints.

However, when returning large objects like this:

struct Huge { ... };

Huge makeHuge() {
    Huge h { x, y, x };
    h.doSomething();
    return h;
}

The following code...

auto h = makeHuge();

... after RVO would be implemented something like this (pseudo code) ...

h_storage = allocate_from_stack(sizeof(Huge));
makeHuge(addressof(h_storage));
auto& h = *properly_aligned(h_storage);

... and makeHuge would compile to something like this...

void makeHuge(Huge* h_storage) // in fact this address can be
                               // inferred from the stack pointer
                               // (or just 'known' when inlining).
{
    phuge = operator (h_storage) new Huge(x, y, z);
    phuge->doSomething(); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM