Jump to content
  • Advertisement
Sign in to follow this  
hick18

rvalues

This topic is 2704 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Regarding this link, I dont understand whats going on.

The new factory method is now

template <class T, class A1>
std::shared_ptr<T>
factory(A1&& a1)
{
return std::shared_ptr<T>(new T(std::forward<A1>(a1)));
}


Yet how does this now still accept lvalues? Doesnt the "A1&&" mean that it can only accept rvalues? And if its an rvalue, why does the forward function need to be called?


template <class T>
struct identity
{
typedef T type;
};

template <class T>
T&& forward(typename identity<T>::type&& a)
{
return a;
}


Same with the definition of the forward function. It takes in an rvalue, and then returns an rvalue.

I dont get it.

Share this post


Link to post
Share on other sites
Advertisement

Yet how does this now still accept lvalues? Doesnt the "A1&&" mean that it can only accept rvalues?

Probably the most through explanation of rvalue references can be found here: http://blogs.msdn.com/b/vcblog/archive/2009/02/03/rvalue-references-c-0x-features-in-vc10-part-2.aspx

Contained therin is this snippit:

· We've already seen how the modifiable lvalue reference, Type& , is willing to bind to modifiable lvalues, but not to anything else (const lvalues, modifiable rvalues, const rvalues).
· We've already seen how the const lvalue reference, const Type& , is willing to bind to everything.
· The modifiable rvalue reference, Type&& , is willing to bind to modifiable lvalues and modifiable rvalues, but not to const lvalues and const rvalues (which would violate const correctness).
· The const rvalue reference, const Type&& , is willing to bind to everything.
[/quote]
Now I haven't delved into rvalue references at all so maybe this doesn't really apply to your situation, but there is a lot of good info in that link so hopefully it helps.

Share this post


Link to post
Share on other sites
Doesnt the "A1&&" mean that it can only accept rvalues?

Not when A1 is a template parameter. In that case A1 will resolve to "foo_type" if the function is passed an rvalue of type "foo_type", and "foo_type (const) &" if it is passed an lvalue of type "foo_type (const)". This feature, combined with std::forward, is what enables perfect forwarding of function parameters.

And if its an rvalue, why does the forward function need to be called?

Once you enter the body of the function the "rvalue" has a name, which means it has become an lvalue. You need to use std::forward to convert it back into an rvalue before passing it to the T constructor.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!