The compiler should automatically perform small functions to automatically create releases. It is much more important to define a move constructor and move a destination. If your arrays are very large and you are performing several operations at the same time, you can also use expression classes to increase the speed of execution.
template <class left, class right> struct AddExpr { const left& _left; const right& _right; AddExpr(const left& Left, const right& Right) :_left(Left), _right(Right) {assert(left.count() == right.count());} int count() const {return _left.count();} int operator[](int index) const {return _left[i]+_right[i];} }; class Array { int* data; int size; int count() const {return size;} Array& operator=(AddExpr expr) { for(int i=0; i<expr.count(); ++i) data[i] = expr[i]; }; AddExpr operator+(const Array& lhs, const Array& rhs) {return AddExpr<Array, Array>(lhs, rhs);} AddExpr operator+(const Array& lhs, const Expr& rhs) {return AddExpr<Array, Expr>(lhs, rhs);} AddExpr operator+(const Expr& lhs, const Array& rhs) {return AddExpr<Expr, Array>(lhs, rhs);} AddExpr operator+(const Expr& lhs, const Expr& rhs) {return AddExpr<Expr, Expr>(lhs, rhs);} int main() { Array a, b, c, d; Array c = (a+b) + (c+d);
This removes all temporary objects and greatly improves cache efficiency. But I completely forgot what is called this technique.
source share