I have a lot of code that is somehow doing exactly the same operations (inherited code), and I want to, while reworking it, compress code without losing functionality. For example let's look at the following functions:
fnc(largetype & a, largetype & b) { f(A); f(B); };
fnc(largetype && a, largetype & b) { f(A); f(B); };
fnc(largetype & a, largetype && b) { f(A); f(B); };
fnc(largetype && a, largetype && b) { f(A); f(B); };
All of them are doing exactly the same thing, but the arguments can be rvalues or lvalues without breaking the function logic. I want to allow the user to pass whatever suits the problem, but I also do not want to copy-paste all code piece by piece. I can do something like this:
fnc(largetype & a, largetype & b) { f(A); f(B); };
fnc(largetype && a, largetype & b) { fnc(a,b) };
fnc(largetype & a, largetype && b) { fnc(a,b) };
fnc(largetype && a, largetype && b) { fnc(a,b) };
which is technically correct, especially with inlining, but seems wrong to me. Is there any other, nicer way to accomplish such an effect?
The only requirements are that types passed as parameters may/will be somehow larger than default memory block size so copy avoidance is crucial. Also there is a non-zero chance that parameter can be smaller but also be a rvalue. Thread safety is optional.
I considered templating these functions, but this is in my opinion also somehow the wrong approach. Templates solve problems with different accepted types. In my case the types are the same, only passed in a different way.