Discussion:
rvalue reference questions
(too old to reply)
Faisal
2010-04-05 06:50:07 UTC
Permalink
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.
Bo Persson
2010-04-05 09:59:13 UTC
Permalink
Post by Faisal
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
In the section Overloading on lvalue / rvalue
<quote>
class Derived
: public Base
{
std::vector<int> vec;
std::string name;
// ...
// ...
// 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.
Post by Faisal
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
Igor Tandetnik
2010-04-05 11:58:11 UTC
Permalink
Post by Faisal
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
Stephan T. Lavavej [MSFT]
2010-04-05 21:13:59 UTC
Permalink
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
Post by Faisal
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
Bronek Kozicki
2010-04-08 11:01:36 UTC
Permalink
Post by Stephan T. Lavavej [MSFT]
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?
Post by Stephan T. Lavavej [MSFT]
The move semantics and perfect
forwarding patterns are unaffected, but my description of how rvalue
references bind to things is now outdated.
B.
Stephan T. Lavavej [MSFT]
2010-04-12 19:32:29 UTC
Permalink
Given template <typename T> whatever foo(T&& t), t is a perfect forwarding
parameter and will bind to anything, both lvalues and rvalues, in both rrv1
and rrv2.

Given anything else, such as whatever bar(X&& x) or template <typename A>
whatever baz(vector<A>&& v), x and v are not perfect forwarding parameters.
In rrv2, they can't bind to lvalues.

STL
Post by Bronek Kozicki
Post by Stephan T. Lavavej [MSFT]
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?
Post by Stephan T. Lavavej [MSFT]
The move semantics and perfect
forwarding patterns are unaffected, but my description of how rvalue
references bind to things is now outdated.
B.
Loading...