I am reading about moving constructors and assignments in Stroustrup's
c++ textbook. Below is a motivating example given by the author. The return
statement in the code illustrates copying a large vector can be inefficient.
Vector operator+(const Vector& a, const Vector& b) {
if (a.size()!=b.size()) throw Vector_size_mismatch{};
Vector res(a.size());
for (int i=0; i!=a.size(); ++i) res[i]=a[i]+b[i];
return res;
}
Question: Wouldn't it be simpler to change the return
type to reference Vector&
to avoid the efficiency issues of copying?