too*_*zzy 6 c++ interface move
假设我有界面:
class MyInterface
{
public:
virtual ~MyInterface() = default;
virtual void DoSomething(const MyType& a, const MyTypeB& b);
};
Run Code Online (Sandbox Code Playgroud)
我想要的是如果任何函数参数是右值或左值引用,则允许使用移动语义。
我不想要的是像这样定义接口:
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);
};
Run Code Online (Sandbox Code Playgroud)
如果向方法中添加更多参数,组合数学会变得更糟。
因此,在实现中,如果我传递了右值,我基本上想移动参数,否则进行复制。
标准库中有std::forward,但它与所谓的“转发引用”一起使用,这需要模板,并且在虚拟方法中不可能有模板参数。
有没有什么方法可以做到这一点,同时保留接口基类型的目的,并且不会使接口本身变得如此膨胀?