Consider a class that is basically an encapsulation of several primitive types.
class sample
{
int a,b,c,d;
public:
sample(const sample& s) : a(s.a), b(s.b), c(s.c), d(s.d) {}
sample(sample&& s) : a(s.a), b(s.b), c(s.c), d(s.d) {}
//other option (by Value)
//inline int compute(sample s)
//{
// return a + b + c + d + s.a + s.b + s.c + s.d;
//}
// By references
inline int compute(const sample& s)
{
return a + b + c + d + s.a + s.b + s.c + s.d;
}
inline int compute(sample&& s)
{
return a + b + c + d + s.a + s.b + s.c + s.d;
}
};
The class overloads compute function for both r-value and l-value references. Here moving the object does not seem to have any benefits over copy.
The only performance gain I can see is that there is no additional copy overhead (As opposed to the single definition that takes argument by value).
Does this implementation make sense? Or should I stick to single function which accepts a value?
Will this implementation cause my binary to increase in size (it should be inlined and hence no-different)