Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arithmetic operators parameter type

It is often stated (e.g. here cppreference) that defining the left hand side (lhs) parameter of an arithmetic operator by value helps optimizing chained operations.

X operator+( X         lhs
           , X const & rhs )

To ensure that I don't accidently change lhs within the function, I like to declare my by value parameters const. Does this change the behavior concerning the desired optimization?

X operator+( X const   lhs
           , X const & rhs )
like image 670
DrPepperJo Avatar asked May 07 '26 05:05

DrPepperJo


1 Answers

Taking by copy is done to enable a specific idiom, when + is implemented in terms of +=:

inline X operator+(X lhs, const X& rhs) {
    lhs += rhs;
    return lhs;
}

On the other hand, if you take lhs by const X& reference, you would have to either make a copy yourself, like this

inline X operator+(const X& lhs, const X& rhs) {
    X res(lhs); 
    res += rhs;
    return res;
}

or to construct a new object, like this:

inline X operator+(const X& lhs, const X& rhs) {
    X res; 
    ... // Modify res to contain the sum of lhs and rhs
    return res;
}

If you are using the idiomatic approach, the compiler can optimize chains of + for you by making a copy once. The compiler notices that when you do this

lhs + rhs1 + rhs2

the result of lhs + rhs1 is a throw-away copy which can be reused in constructing (lhs + rhs1) + rhs2 without performing a copy again.

On the other hand, if you use one of the alternatives above, the compiler would need to make a copy for each operation in the chain.

like image 162
Sergey Kalinichenko Avatar answered May 09 '26 03:05

Sergey Kalinichenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!