Consider this code:
#include <iostream>
using namespace std;
void Func(int&& i) {
++i;
}
int main() {
int num = 1234;
cout << "Before: " << num << endl;
Func(std::move(num));
cout << "After: " << num << endl;
}
Its output is:
Before: 1234
After: 1235
Clearly, i
is being modified inside Func
, as it is bound to parameter i
after being "converted" to an r-value reference by std::move
.
Well, my point:
Moving an object means transferring ownership of resources from one object into another. However, built-in types holds no resources because they themselves are the resources. It makes no sense to transfer the resources they hold. As shown by the example, num
's value is modified. Its resource, its self, is the one being modified.
Do built-in types have move semantics?
Also, Do built-in type objects after it is moved (if it is) a well-defined behavior?
std::move
doesn't move. Your example does not have a single move between the outputs. – Dylanstd::move(some_lvalue)
casts an lvalue to an rvalue, thus enabling a subsequent move." and "Moving is exclusively performed by the move constructor, not bystd::move
, and not by merely binding an rvalue to an rvalue reference." – Ectomorph