I read the cppreference page for operator overloading – “binary arithmetic operators”. I was confused by a comment in a piece of code, which encouraged passing by value, instead of reference, for compiler optimisation. I’m not experienced so I might’ve missed some information, but to my knowledge, passing by reference is encouraged over passing by value when possible.
First example:
class A {
public:
A& operator+=(A const& rhs) {
/*...*/
return *this;
}
friend A operator+(A lhs, A const& rhs) {
lhs += rhs;
return lhs;
}
};
In this code, it is stated that passing lhs
by value in operator+
helps to optimise chained addition. This, to my understanding, stands in contrast to this answer on SO, which states that passing by value prevents “named return value optimisation”.
Second example:
inline A operator+(A const& lhs, X const& rhs) {
A a = lhs;
a += rhs;
return a;
}
The second example provides A a = lhs;
, which, explained by the accepted answer to this thread, states that the optimisation of the first example is true if operator+
is implemented in terms of operator+=
.
Is the optimisation of the first example true if operator+
is implemented without operator+=
?
Which of these implementations are preferred for computational efficiency, provided that the second example includes “named return value optimisation” ?
Is there any other reason to prefer one over the other?