Need help with simple move constructor

This puppy refuses to compile and I'm getting this error message

In constructor 'X::X(X&&)':
7:27: error: cannot bind 'X' lvalue to 'X&&'
3:10: note: initializing argument 1 of 'void X::move_from(X&&)'


1
2
3
4
5
6
7
8
class X{
    int x{};
    void move_from(X&& x2){x = x2.x;}
public:
    
    X(int i) : x{i}{}
    X(X&& x2){move_from(x2);}
};


As I see it everything should work. Looks like argument types are the same for me to call move_from with X&&. But why is is not working?
Hi,

Have a look at the example in this, it uses std::move

http://en.cppreference.com/w/cpp/language/move_constructor


Tnx for reply man, you were right this worked
1
2
3
4
5
6
7
8
9
10
11
12
#include<utility>

class X{
    int x{};
    void move_from(X&& x2){x = x2.x;}
public:
    
    X(int i) : x{i}{}
    X(X&& x2){move_from(std::move(x2));}
};

int main(){}


Just 1 thing I want to ask. Why exactly x2 type on line 9 is not interpreted as X&& but instead its being used as just X (or X& idk)?
Last edited on
Just wondering why you have the move_from function at all?

Why exactly x2 type on line 9 is not interpreted as X&& but instead its being used as just X (or X& idk)?


Apart from: The argument to std::move is an lvalue, I don't know :+(
Why exactly x2 type on line 9 is not interpreted as X&& but instead its being used as just X (or X& idk)?

Imagine what could happen if you passed x2 to some other function that has been overloaded for both const X& and X&&.

1
2
3
4
5
X(X&& x2)
{
	someFunction(x2);
	move_from(std::move(x2));
}

If x2 was passed as X&& to someFunction the state of x2 might change (because it has been moved from) so when you pass it to the second function (move_from) it is not receiving the same state as the object had when the move constructor began.


An object is only passed as a rvalue-reference implicitly when you pass a temporary object because it is known that the object can't be used afterwards.

1
2
3
// here the object created by X() is passed as a X&& because 
// there is no way the object could be used after this line of code.
someFunction(X());

In all other cases you need to explicitly convert it to a rvalue-reference if that is what you want.
Im completing an example from book and there move constructor and copy constructor is implemented using copy_from and move_from coz they might be useful doing other stuff as well.

Tnx you both guys very much!!! I think I understand now :)
Last edited on
A named rvalue reference behaves just like a reference semantically. If it helps, you should consider it necessary to use std::move to get move semantics whenever the variable is named (except in the case of returning a local variable from a function, where the compiler can deduce the need unambiguously.)
Tnx to you too man! Really appreciate all the answers!
EDIT : and now when I look in the book move_from didnt really take rvalue reference as parameter but just lvalue reference!
Last edited on
Topic archived. No new replies allowed.