C++0x and R-Value References

Published June 15, 2009
Advertisement
Really, lvalue references (T&) are a specialized version of rvalue references (T&&). This much is clear because T&& can be assigned to both l and r value references, whereas T& strictly assigns to lvalue refs. Moreover, when T is a template parameter, T&& will become Type& or Type&& depending on if T is an l or r value ref, whereas T& forces everything to an lvalue ref. Of course, both will remain the same if T is an ordinary type. In other words, T&& discriminates between l and r values, whereas T& does not. Clearly, T&& is more general.

In template parameter deduction, this fact also holds. T& always requires an lvalue ref, works naturally with lvalues and their refs, but rvalues and their refs will convert into const Type&, causing T to be deduced to const Type. On the other hand, T&& works naturally with rvalues and their refs, but with lvalues and their refs T becomes [const] Type&, so that T&& will collapse into [const] Type& &&=[const] Type&. In this way, T&& is again able to discriminate between l and r values, whereas T& cannot.

To be clear, what T&& really represents is not so much a new kind of reference. It covers all the functionality of T&, and they are often interchangeable. However, it is a more general form of the C++ reference that allows access to modifiable rvalues and can discriminate between l and r values.

One more perspective: T&& behaves exactly like the underlying type, be it lvalue, rvalue, Type, Type& or Type&&. However, T& is not exact. It behaves the same as the underlying type, but it makes everything look like an l value, discarding any r-valueness. So again, T& shows itself as a derivative of T&&, and T&& shows itself as the more general form.
Previous Entry Reflections on Agile
Next Entry Parallel Languages
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement
Advertisement