Sign in to follow this  

rvalues

This topic is 2490 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 [url="http://www.artima.com/cppsource/rvalue.html"]this link[/url], I dont understand whats going on.

The new factory method is now

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

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?

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

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

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
[quote name='hick18' timestamp='1298153951' post='4776451']
Yet how does this now still accept lvalues? Doesnt the "A1&&" mean that it can only accept rvalues?
[/quote]
Probably the most through explanation of rvalue references can be found here: [url="http://blogs.msdn.com/b/vcblog/archive/2009/02/03/rvalue-references-c-0x-features-in-vc10-part-2.aspx"]http://blogs.msdn.com/b/vcblog/archive/2009/02/03/rvalue-references-c-0x-features-in-vc10-part-2.aspx[/url]

Contained therin is this snippit:
[quote]
· 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
[quote name='hick18' timestamp='1298153951' post='4776451']Doesnt the "A1&&" mean that it can only accept rvalues?[/quote]
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) [b]&[/b]" 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.

[quote name='hick18' timestamp='1298153951' post='4776451']And if its an rvalue, why does the forward function need to be called?[/quote]
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

This topic is 2490 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.

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

Sign in to follow this