I don't like to repeat myself in code, but also I don't want to lose performance by simple functions. Suppose the class has operator+
and function Add
with same functionality (considering former as handy way of using class in expressions and latter as "expilicit" way to do so)
struct Obj {
Obj operator+(float);
Obj Add(float);
/* some other state and behaviour */
};
Obj AddDetails(Obj const& a, float b) {
return Obj(a.float_val + b, a.some_other_stuff);
}
Obj Obj::operator+(float b) {
return AddDetails(*this, b);
}
Obj Obj::Add(float b) {
return AddDetails(*this, b);
}
For the purpose of making changes easier both functions are implemented with auxiliary function call. Therefore, any call to operator makes 2 calls what is not really pleasant.
But is compiler smart enough to eliminate such double calls?
I tested with simple classes (that contain built-in types and pointers) and optimizer just doesn't calculate something not needed, but how does it behave in large systems (with hot calls especially)?
If this is where RVO takes place, then does it work in larger sequences of calls (3-4) to fold it in 1 call?
P.S. Yes, yes, premature optimization is the root of all evil, but still I want an answer