So let's say i have interface:
class MyInterface
{
public:
virtual ~MyInterface() = default;
virtual void DoSomething(const MyType& a, const MyTypeB& b);
};
And the thing i want is to allow usage of move semantics if any of function arguments is rvalue or lvalue reference.
What i don't want is to define interface like this:
class MyInterface
{
public:
virtual ~MyInterface() = default;
virtual void DoSomething(const MyType& a, const MyTypeB& b);
virtual void DoSomething(MyType&& a, const MyTypeB& b);
virtual void DoSomething(const MyType& a, MyTypeB&& b);
virtual void DoSomething(MyType&& a, MyTypeB&& b);
};
And the combinatorics get even worse if more parameters added to the method.
So in implementation i basically want to move argument if i got rvalue passed and copy otherwise.
There's std::forward
in the standard library, but it works with so called "forwarding reference", which require templates and it's impossible to have template parameters in virtual methods.
Is there any way to do this with preserving the purpose of interface base type and without bloating the interface itself so much?
void DoSomething(MyType a, MyType b);
. That way the caller can decide whether to move or copy, as appropriate for the callers needs. (Some of our methods have 6+ parameters, and having all permutations ofMyType&&
andMyType const&
would be unmanageable.) – Recipeconst
references. That's what they'll always be. The End. – WooDoSomething
not know if it wants to move from a particular parameter or not? Why is that not simply part of what the function does, part of its inherent nature? – Incandescestd::vector::push_back
. – Valentijnmi.DoSomething(a, b);
both will be call-by-copy, but if the caller calls itmi.DoSomething(move(a), move(b));
both will be call-by-move. Assuming MyType has both copy and move semantics. – Recipe