From: Faisal on 5 Apr 2010 02:50 I was reading about rvalue refernces in this link http://www.artima.com/cppsource/rvalue.html Most of the thing seems clear to me. But I'm confused in certain parts For eg: In the section Overloading on lvalue / rvalue <quote> class Derived : public Base { std::vector<int> vec; std::string name; // ... public: // ... // move semantics Derived(Derived&& x) // rvalues bind here : Base(std::move(x)), vec(std::move(x.vec)), name(std::move(x.name)) { } Derived& operator=(Derived&& x) // rvalues bind here { Base::operator=(std::move(x)); vec = std::move(x.vec); name = std::move(x.name); return *this; } // ... }; Note above that the argument x is treated as an lvalue internal to the move functions, even though it is declared as an rvalue reference parameter. That's why it is necessary to say move(x) instead of just x when passing down to the base class. This is a key safety feature of move semantics designed to prevent accidentally moving twice from some named variable. All moves occur only from rvalues, or with an explicit cast to rvalue such as using std::move. If you have a name for the variable, it is an lvalue. </Quote> In this case how the compiler convert the rvalue reference parameter as lvalue? And when the question of double-moving comes? Can someone give an example.
From: Bo Persson on 5 Apr 2010 05:59 Faisal wrote: > I was reading about rvalue refernces in this link > http://www.artima.com/cppsource/rvalue.html > > Most of the thing seems clear to me. But I'm confused in certain > parts > > For eg: > In the section Overloading on lvalue / rvalue > > <quote> > > class Derived > : public Base > { > std::vector<int> vec; > std::string name; > // ... > public: > // ... > // move semantics > Derived(Derived&& x) // rvalues bind here > : Base(std::move(x)), > vec(std::move(x.vec)), > name(std::move(x.name)) { } > > Derived& operator=(Derived&& x) // rvalues bind here > { > Base::operator=(std::move(x)); > vec = std::move(x.vec); > name = std::move(x.name); > return *this; > } > // ... > }; > > > Note above that the argument x is treated as an lvalue internal to > the move functions, even though it is declared as an rvalue > reference parameter. That's why it is necessary to say move(x) > instead of just x when passing down to the base class. This is a > key safety feature of move semantics designed to prevent > accidentally moving twice from some named variable. All moves occur > only from rvalues, or with an explicit cast to rvalue such as using > std::move. If you have a name for the variable, it is an lvalue. > </Quote> > > In this case how the compiler convert the rvalue reference parameter > as lvalue? There is no conversion, it is just treated as an lvalue. Like the quote says, an rvalue is usually an unnamed temporary. The parameter is not, but the value it is bound to is the rvalue. > And when the question of double-moving comes? Can someone give an > example. You can just think about what happens if you try to move from a value twice: vec1 = std::move(x.vec); vec2 = std::move(x.vec); Here vec1 gets the value from x.vec, which is then empty. Then vec2 gets nothing! On the other hand, here vec1 = x.vec; vec2 = x.vec; vec1 and vec2 both get a copy of x.vec (which also retains its value). Bo Persson
From: Igor Tandetnik on 5 Apr 2010 07:58 Faisal wrote: > I was reading about rvalue refernces in this link > http://www.artima.com/cppsource/rvalue.html The clearest explanation of rvalue references I've seen so far: http://blogs.msdn.com/vcblog/archive/2009/02/03/rvalue-references-c-0x-features-in-vc10-part-2.aspx -- With best wishes, Igor Tandetnik With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
From: Stephan T. Lavavej [MSFT] on 5 Apr 2010 17:13 Note that the VC10 CTP (which was pre-Beta 1) supported "rvalue references v1" like GCC 4.3 and 4.4 did, while VC10 RTM supports "rvalue references v2" like GCC 4.5 does. The move semantics and perfect forwarding patterns are unaffected, but my description of how rvalue references bind to things is now outdated. Stephan T. Lavavej Visual C++ Libraries Developer "Igor Tandetnik" <itandetnik(a)mvps.org> wrote in message news:OrL%23HdL1KHA.4204(a)TK2MSFTNGP04.phx.gbl... Faisal wrote: > I was reading about rvalue refernces in this link > http://www.artima.com/cppsource/rvalue.html The clearest explanation of rvalue references I've seen so far: http://blogs.msdn.com/vcblog/archive/2009/02/03/rvalue-references-c-0x-features-in-vc10-part-2.aspx -- With best wishes, Igor Tandetnik With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
From: Bronek Kozicki on 8 Apr 2010 07:01 On 05/04/2010 22:13, Stephan T. Lavavej [MSFT] wrote: > Note that the VC10 CTP (which was pre-Beta 1) supported "rvalue > references v1" like GCC 4.3 and 4.4 did, while VC10 RTM supports "rvalue > references v2" like GCC 4.5 does. do you mean that "T&&" parameter no longer binds to lvalue in VC10? > The move semantics and perfect > forwarding patterns are unaffected, but my description of how rvalue > references bind to things is now outdated. B.
|
Next
|
Last
Pages: 1 2 Prev: Mailing follow-ups (was: Re: VC++ 6.0 vs VC++ 8.0) Next: Legacy Code update problems. |