Why do you think there was a copy? You asked the compiler to generate a move constructor for you. The generated move constructor will do a memberwise move of all elements. There are basically two scenarios:
- The corresponding member has a move constructor in which case this move constructor will be applied.
- The corresponding member has no move constructor in which case the copy constructor will be applied.
Moving built-in types is like copying them, i.e., they don't do anything to source of the move unlike, e.g., std::unique_ptr<T>
which will put a null-pointer into the source.
When you used
A b = std::move(a);
you give a
the appearance of a temporary object which can be move constructed, using the generated move constructor for A
. The rvalue-reference isn't constructed but the same object is simply cast. std::move(a)
is equivalent to static_cast<A&&>(a)
.