struct Base
{
Base(int a_) : a(a_)
{
}
int a;
};
class Derived: public Base
{
public:
Derived(int i): Base(i)
{
}
Derived(const Derived && rhs)
: Base(std::move(rhs))
{
}
};
int main()
{
Derived d1(2);
Derived d2 = std::move(d1);
std::cout << d1.a << '\n';
}
Asked
Active
Viewed 36 times
0

alexander.sivak
- 4,352
- 3
- 18
- 27
-
99.99999% of all move constructors accept a non-`const` rvalue reference. Why is yours the exception? – Ben Voigt Aug 19 '21 at 15:00
-
Does this answer your question? [What is move semantics?](https://stackoverflow.com/questions/3106110/what-is-move-semantics) – Mgetz Aug 19 '21 at 15:01
-
`std::move` is a **cast**. It doesn't actually move anything. By the way, there is no such thing as "undefined integer value" that is distinct from all normal integer values, so it is a bit unclear what exactly you expected. – n. m. could be an AI Aug 19 '21 at 16:15
-
@n.1.8e9-where's-my-sharem., I thought the whole point of move semantics was to apply it only where it "doesn't matter," in the sense that the thing from which we move dies and goes away right after the moving. http://thbecker.net/articles/rvalue_references/section_05.html – alexander.sivak Aug 19 '21 at 16:20
-
"Move semantics" means taking resources from objects that don't need them any more (either because they are going to die, or for any other reason). `std::move` doesn't do any moving. It marks the object as OK to move from. The actual moving is done elsewhere, usually in move constructors/assignment operators. However `int` isn't much of a resource. It is essentially free to copy, so it makes no sense to do any special moving for it that is distinct from copying. – n. m. could be an AI Aug 19 '21 at 16:28