设计一个接口时, 如需传递一个对象,然后保存起来。
让它即能接受左值, 又能接受右值
- 写两个重载
explicit A(string&& a) : _a(move(a)) {}
explicit A(const string& a) : _a(a) {}
上面的一次move construct, 下面的一次copy construct, 但写两个太麻烦
- 用perfect forwarding
template<typename T>
explicit A(T&& a) : _a(forward(a)) {}
这样实现只能写在头文件, 且可以传各种奇怪类型进来。 而且perfect forwarding有些情况不能用,例如:braced initializers, 0 or NULL, 只有声明的数字const static成员, 有重载或有模板的函数做参数,bitfields
- 直接按值传递,然后在里面用move
explicit A(string a):_a(move(a)) {}
这样外面用的时候, 直接用右值是一次move construct, 用move转右值是两次move construct, 用左值是一次copy construct, 一次move construct, 算算只多了一次move construct, 但这个开销是很低的, 所以结论是应该这样写
另外如果上层是const &之类, 用move也是不能转成右值的
void test(const string& a)
{
A b(move(a));
}
网友评论