I have two related questions, both related to how to handle lvalue and rvalue references uniformly.
Virtual function case:
struct Foo {
Object m_object;
virtual void bar(SomeRefTypeOfObject object);
};
Basically, what I want to achieve is to have a SomeRefTypeOfObject, which is able to store both lvalue and rvalue reference to Object. bar is a larger function, and it will use one m_object = object; statement store the value of object (a copy or move operation, depending on the type of the stored reference). The reason is that I want to avoid having two bar functions (for each reference type). Is there anything in the standard library which can do this conveniently and efficiently, or do I have to roll my own solution for this? If I have to roll my own solution, how would a good implementation look like?
Template function case:
struct Foo {
Object m_object;
template <...>
void bar(... object);
};
I'd like to have a bar template, which can be called with any kind of Object or its derived classes/other objects which can be converted to Object (like if bar were two overloaded functions with const Object & and Object && parameters), but instantiated with only const Object & and Object &&. So, I don't want to have bar instantiated for each derived type. What is the most clear way to do that? I suppose I'll need some form of SFINAE here.
Note: m_object = object; assignment can happen in a function which is called by bar. So the solution passing object by value is not optimal, as unnecessary copies/moves will be made (there can be types for which move is not that cheap as passing down a reference). Furthermore, the assignment can happen in a conditional way, so passing object by value just in the leaf function not a solution (because it can be runtime dependent, which function does the actual assignment).