1

That is, how much performance improvement one typically gets in an otherwise well-designed C++03 code when one takes advantage of C++11 typename && features such as move constructors, etc.? How often it's worth the trouble, and in what circumstances?

EDIT:

Imagine an alternative universe where instead of rvalue references C++11 extended C++03 with a keyword "returning" that marks a local variable that would get returned by a function. If a local variable is marked as "returning" it's placed in the portion of the stack where the return value would usually reside. Imagine you could use that keyword like this:

SomeClass foo()
{
    returning SomeClass bar; // Created where return value usually resides.
    // do something with bar
    return bar; // No copy constructor is involved here.
}

I think such mechanism would enable creation of make_unique and such just as well as rvalue references do, and would cause much less confusion and potential pitfalls. Rule of three would remain rule of three instead of becoming rule of five; there would be no confusion of whether && if rvalue ref or universal ref; there would be no need for std::move etc.

What am I missing here?

Michael
  • 5,195
  • 1
  • 28
  • 46
  • 2
    You are being rapidly voted as "too broad", but I don't think it should discourage your line of inquiry. Maybe pick a couple of focused cases and ask something very specific about them? – HostileFork says dont trust SE Jun 03 '15 at 18:59
  • 2
    Measure. [Measure](https://www.youtube.com/watch?v=BezbcQIuCsY). Measure. – fredoverflow Jun 03 '15 at 18:59
  • 2
    It's not so much that that it makes things perform better, it's that it makes it much cleaner and easier to avoid needless allocate/copy/free cycles, resulting (we hope) in clearer code that's easier to understand and maintain. Take a look at some of the pre-C++11 monstrosities that people have constructed to get what move semantics give you naturally. For example, compare `auto_ptr` and `unique_ptr`. – David Schwartz Jun 03 '15 at 19:02
  • The value of an rvalue reference is whatever you set it to, using the assignment operator of course. – Carlton Jun 03 '15 at 19:06
  • 1
    What you suggest in the edit as an alternative universe is known as RVO, and is already implemented by most decent compilers. – Suma Jun 03 '15 at 20:23

1 Answers1

7

The value of rvalue references does not not primarily lie in performance. Much rather, it lies in expressiveness. Without rvalue references, you cannot encode ownership in the type system in a useful way. Thanks to transfer of ownership that rvalue references allow, we can now write fully subexpression-wise correct code that has no non-local dependencies.

For example, pre-C++11:

int * p = new int[N];    // may be a bug, who knows?

                         // ... keep paying attention...

delete p;                // ah, no bug... or is it?

With rvalues:

auto p = std::make_unique<int[]>(N);   // fine

Note that no single subexpression of the modern example is itself ever the root cause of a resource bug. The same approach can be applied to a myriad of resource/handle pairs: memory/pointer, file/handle, mutex/lock, task/continuation, etc.

(If you think of resource acquisition and release as two opposite poles, then modern C++ should be written like a magnet: the two are always together as part of one entity.)

Allowing the type system to completely guarantee locality of correctness is a dramatic improvement of the language.

Kerrek SB
  • 428,875
  • 83
  • 813
  • 1,025
  • 3
    Plus one as usual. If I could afford you I'd try to hire you! – Bathsheba Jun 03 '15 at 19:04
  • 1
    @Bathsheba Why hire someone who'll solve your problems for free? :-) – HostileFork says dont trust SE Jun 03 '15 at 19:06
  • @Kerrek, could you explain why rvalue references are essential to your example? I get the value of unique_ptr, shared_ptr, and their make_; these are indeed dramatic improvements over new/delete. What I don't understand is why rvalue references are essential for that. Actually, give me 2 min, I'll add an alternative (completely fictitious) extension of C++03. – Michael Jun 03 '15 at 19:58
  • @Michael: I don't quite follow. Is your question now why one language extension was chosen instead of another? I don't know, I wasn't there. Did you submit your design as a proposal? Only things that are proposed get accepted. – Kerrek SB Jun 03 '15 at 20:14
  • 1
    @KerrekSB , disregard the edit; the question is: why rvalue references are essential in your example? Why cannot one implement make_unique without them? – Michael Jun 03 '15 at 20:26