Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Static cast to rvalue reference

28 views
Skip to first unread message

Paul

unread,
Jun 12, 2015, 9:13:07 AM6/12/15
to
I'm confused by the syntax below. Why is the syntax for this static_cast not
X x1;
X&& x2 = static_cast<X&&>(x1);
x2 has been cast to the type X&& so why does the syntax make it seem as though x2 is of type X after the cast?

Many thanks for your help.

Paul


CODE THAT CONFUSES ME IS BELOW
X x1;
X x2 = static_cast<X&&>(x1);

Öö Tiib

unread,
Jun 12, 2015, 11:03:58 AM6/12/15
to
You take the 2 lines of code out of context of what it
illustrates so how we can know what its purpose is?

The second line that confuses you makes perfect sense,
but is usually written like that:

X x2 = std::move(x1);

That does same thing so perhaps it was some text
about move semantics.


Paul

unread,
Jun 12, 2015, 11:40:08 AM6/12/15
to
Yes, it was about move semantics. Here's a better way of rephrasing the question. The code below compiles but gives a runtime crash.
The way to fix this is to delete the && from std::vector<std::string> && tmp = ...

My question is why this fix is necessary. Why doesn't the code below do the swap? Thank you very much for your help.

Paul

void swap( std::vector<std::string> & x, std::vector<std::string> & y )
{
std::vector<std::string>&& tmp = static_cast<std::vector<std::string> &&>( x );
x = static_cast<std::vector<std::string> &&>( y );
y = static_cast<std::vector<std::string> &&>( tmp );
}

int main()
{

std::vector<std::string> vecStr1{"hello", "world"};
std::vector<std::string> vecStr2{"heh", "one"};
swap(vecStr1, vecStr2);
std::cout<<vecStr1[0];
}

Martin Shobe

unread,
Jun 12, 2015, 12:27:26 PM6/12/15
to
rvalue references are still references. tmp and x refer to the same
object. When you do the first assignment, the contents of y become the
contents of x and y becomes limited in what you can do with it until you
assign to it. When you do the second assignment, the contents of y
becomes the contents tmp (i.e. x) and tmp (x) becomes limited in what
you can do with it.

When you remove the &&, tmp becomes a copy of x, so the second move
limits tmp but not x.

Martin Shobe

Paavo Helde

unread,
Jun 12, 2015, 1:08:16 PM6/12/15
to
Paul <peps...@gmail.com> wrote in
news:854ab5c3-7dbf-41be...@googlegroups.com:
>
> My question is why this fix is necessary. Why doesn't the code below
> do the swap? Thank you very much for your help.
>
> Paul
>
> void swap( std::vector<std::string> & x, std::vector<std::string> & y
> )
> {
> std::vector<std::string>&& tmp =
> static_cast<std::vector<std::string> &&>( x ); x =
> static_cast<std::vector<std::string> &&>( y ); y =
> static_cast<std::vector<std::string> &&>( tmp );
> }

For moving around vector contents you need an actual place to hold a
vector content meanwhile, having any number of references is not enough.

A car analogy: you have 2 stolen cars hidden in 2 trucks. You need to
swap these cars so that nobody can see a car. Each truck can hold 1 car
only, but you can manouver them so that a car can drive directly from one
truck into another (empty) truck. You have unlimited number of fake
license plates (references) for trucks and cars. How to solve?

Hint: moving license plates around does not solve the problem, you need
another (empty) truck.

hth
Paavo
0 new messages