Jump to content
Sign in to follow this  
  • entries
    14
  • comments
    12
  • views
    14050

C++0x and R-Value References

Sign in to follow this  
DudeMiester

283 views

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.
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!