I'm reading through Bjarne Stroustrup's The C++ Programming Language (4th edition) and on p. 516 he says:
How does the compiler know when it can use a move operation rather than a copy operation? In a few cases, such as for a return value, the language rules say that it can (because the next action is defined to destroy the element)
Also on p. 517 he says:
[The object] has a move constructor so that "return by value" is simple and effecient as well as "natural"
If return always uses a move operation, then why doesn't something like the following work?
#include <vector>
#include <assert.h>
using namespace std;
vector<int> ident(vector<int>& v) {
return v;
};
int main() {
vector<int> a {};
const vector<int>& b = ident(a);
a.push_back(1);
assert(a.size() == b.size());
}
Shouldn't a and b be be pointing to the same objects?