想象一下,你有很多重载的方法(在C++ 11之前)看起来像这样:
class MyClass {
public:
void f(const MyBigType& a, int id);
void f(const MyBigType& a, string name);
void f(const MyBigType& a, int b, int c, int d);
// ...
};
Run Code Online (Sandbox Code Playgroud)
这个函数复制了a(MyBigType),所以我想通过提供一个版本f来移动a而不是复制它来添加优化.
我的问题是,现在f重载次数将重复:
class MyClass {
public:
void f(const MyBigType& a, int id);
void f(const MyBigType& a, string name);
void f(const MyBigType& a, int b, int c, int d);
// ...
void f(MyBigType&& a, int id);
void f(MyBigType&& a, string …Run Code Online (Sandbox Code Playgroud)