A question about std::move

Why the b is 3 after I move it? I think it is an uncertain value.

1
2
3
4
5
6
7
 int main()
{
	int a = 2, b = 3;
	a = std::move(b);
	cout << b;
	system("pause");
}


From C++ Primer 5th:
int&&rr3 = std::move(rr1);
Calling move tells the compiler that we have an lvalue that we want to treat as if it were an rvalue. It is essential to realize that the call to move promises that we do not intend to use rr1 again except to assign to it or to destroy it. After a call to move, we cannot make any assumptions about the value of the moved-from object.
For POdTypes http://en.cppreference.com/w/cpp/concept/PODType there is no difference between move and copy (trivial copy constructor, trivial move constructor, trivial copy assignment operator, trivial move assignment operator).
Before you move on to types where there is a difference between a move and a copy, note that a read operation after an object has been moved is undefined behavior without an intervening assignment, so whatever you observe on line 5 is pointless to speculate about from a standards point-of-view.
Last edited on
> a read operation after an object has been moved is undefined behavior without an intervening assignment

Not necessarily undefined behaviour, even for types that are not trivially moveable (for instance std::string).

Moved-from state of library types

Objects of types defined in the C ++ standard library may be moved from. Move operations may be explicitly specified or implicitly generated. Unless otherwise specified, such moved-from objects shall be placed in a valid but unspecified state. -IS

Not necessarily undefined behaviour, even for types that are not trivially moveable (for instance std::string).

Thanks for the correction.

Still, not worth speculation from a standards point-of-view. I can see a string implementation with the small-string-optimization possibly having completely unchanged state after a move which might make the OP wonder. Point being, you can't depend on what you observe to be meaningful (or consistent.)
Topic archived. No new replies allowed.