c++ 接口对象参数传递设计

2018-06-17 22:37:13来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

设计一个接口时, 如需传递一个对象,然后保存起来。

让它即能接受左值, 又能接受右值

  1.  写两个重载
explicit A(string&& a) : _a(move(a)) {}
explicit A(const string& a) : _a(a) {}

  上面的一次move construct, 下面的一次copy construct, 但写两个太麻烦 

  2. 用perfect forwarding

template<typename T>
explicit A(T&& a) : _a(forward(a)) {}

  这样实现只能写在头文件, 且可以传各种奇怪类型进来。 而且perfect forwarding有些情况不能用,例如:

braced initializers, 0 or NULL, 只有声明的数字const static成员, 有重载或有模板的函数做参数,bitfields

  3. 直接按值传递,然后在里面用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));    // 还是左值引用, 因为 && & -> &
}

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:P1865 A % B Problem

下一篇:第四讲 树(中)