I see lots of people having problems understanding rvalue references. Will "ordinary" C++ code (e.g., that uses the standard library, but doesn't implement it) need to know about rvalue references once the new standard comes out, or can the people/code just pretend rvalue references don't exist? Do rvalue references primarily concern library implementors or all programmers?
Rvalue references is a small technical extension to the C++ language. Rvalue references allow programmers to avoid logically unnecessary copying and to provide perfect forwarding functions. They are primarily meant to aid in the design of higer performance and more robust libraries.
If the function argument is an rvalue, the compiler deduces the argument to be an rvalue reference. For example, assume you pass an rvalue reference to an object of type X to a template function that takes type T&& as its parameter. Template argument deduction deduces T to be X , so the parameter has type X&& .
“l-value” refers to a memory location that identifies an object. “r-value” refers to the data value that is stored at some address in memory. References in C++ are nothing but the alternative to the already existing variable. They are declared using the '&' before the name of the variable.
An rvalue is an expression that is not an lvalue. Examples of rvalues include literals, the results of most operators, and function calls that return nonreferences. An rvalue does not necessarily have any storage associated with it.
I didn't see anything much to disagree with in GMan's (aka gmannickg) blog article on the subject:
http://blackninjagames.com/?p=95
Summary: anyone who knows about copy-and-swap (and in general the rule of three), should know about move semantics (and hence rvalue references), because the best-practice idioms change as a result. I think, therefore, that's anyone who cares at all about C++.
I don't think you have to know, because of course copy-and-swap still works, it's still exception-safe, and so on. And I suppose someone could argue whether "ordinary" C++ code and programmers even need to know copy-and-swap. I'd hope that the answer is "well, duh, obviously", but you never know.
Of course, the point at which one can start using C++0x will vary from place to place. It's probably not a good idea to unlearn C++03 any time soon.
For example, returning huge collections by value in contexts that aren't valid for copy elision becomes a better bet if you can rely on move assignment. This isn't a case of something that's invalid in C++03 becoming valid in C++0x, it's code that's heinously slow in C++03, that you'd work around (even if only with a well-placed swap
), becoming fine in C++0x. So if you pretend rvalue references don't exist, you'll carry on working around something you don't need to. If you make the jump then it might backport cleanly to C++03 but run dog-slow, which is a pain.
So the smart thing might be to know about it, and in many contexts pretend it doesn't exist.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With