Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What kinds of code need to be aware of rvalue references?

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?

like image 748
fredoverflow Avatar asked Nov 13 '10 17:11

fredoverflow


People also ask

What are rvalue references used for?

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.

How do you use rvalue reference in C++?

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&& .

What is the difference between L value and R value references?

“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.

What is a C++ rvalue?

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.


1 Answers

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.

like image 180
Steve Jessop Avatar answered Sep 19 '22 08:09

Steve Jessop