std :: move里面的移动赋值运算符 [英] std::move inside move assignment operator

查看:440
本文介绍了std :: move里面的移动赋值运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了另一个问题,当实现移动构造函数时,这是一个好习惯,std ::移动每个成员初始化列表,因为如果成员恰好是另一个对象,那么该对象的移动构造函数将被调用。像这样...

  //移动构造函数
Car :: Car(Car& obj)

prBufferLength(std :: move(obj.prBufferLength)),
prBuffer(std :: move(obj.prBuffer))
{
obj.prBuffer = nullptr;
obj.prBufferLength = 0;
}

但是在我看过的所有示例移动赋值运算符中,没有提到使用std :: move的原因相同。如果成员是一个对象,那么应该使用std :: move?像这样...

  //移动作业
Car Car :: operator =(Car&& obj )
{
delete [] prBuffer;

prBufferLength = std :: move(obj.prBufferLength);
prBuffer = std :: move(obj.prBuffer);

obj.prBuffer = nullptr;
obj.prBufferLength = 0;
return * this;
}

UPDATE: b

我知道在我选择的示例中没有必要使用std :: move(糟糕),但是如果成员是对象,我感兴趣。


<阅读链接问题后,我可以看到第二个最高评价的答案是使用 std :: move 在移动构造函数的初始化器列表中,因为无论它是否是原始类型,它都会做正确的事情。我有点不同意,认为你应该只调用 std :: move 在适当的,但这是个人喜好来进来。



此外,对于你的移动赋值运算符,你的方式是好的,虽然我认为不必要的调用 std :: move 应该删除个人。另一个选择是使用 std :: swap ,这将为您做正确的事。

  Car Car :: operator =(Car& obj)
{
std :: swap(this-> prBufferLength,obj.prBufferLength);
std :: swap(this-> prBuffer,obj.prBuffer);
return * this;
}

上述移动赋值操作符和移动赋值操作符的区别在于内存的释放延迟,而您的版本立即释放内存,这在某些情况下可能很重要。


I read in another question that when implementing a move constructor it is good practice to std::move each member in the initializer list because if the member happens to be another object then that objects move constructor will be called. Like so...

//Move constructor
Car::Car(Car && obj)
    : 
    prBufferLength(std::move(obj.prBufferLength)), 
    prBuffer(std::move(obj.prBuffer)) 
{
    obj.prBuffer = nullptr;
    obj.prBufferLength = 0;
}

However in all the sample move assignment operators I've seen, there has been no mention of using std::move for the same reasons. If the member is an object then should std::move be used? Like so...

//Move assignment
Car Car::operator=(Car && obj)  
{
    delete[] prBuffer;

    prBufferLength = std::move(obj.prBufferLength);
    prBuffer = std::move(obj.prBuffer);

    obj.prBuffer = nullptr;
    obj.prBufferLength = 0;
    return *this;
}

UPDATE:

I appreciate there is no need to use std::move in the example I have chosen (poorly) however I'm interested in if the members were objects.

解决方案

After reading the linked question, I can see the advice in the second most-upvoted answer is to use std::move in the initializer list for the move constructor because no matter if it is a primitive type or not, it will do the right thing. I somewhat disagree with that and think you should only call std::move where appropriate, but this is were personal preferences come in.

Also, for your move assignment operator, the way you have it is fine although I think the unnecessary call to std::move should be removed personally. Another option is to use std::swap which will do the right thing for you.

Car Car::operator=(Car && obj)  
{
    std::swap(this->prBufferLength, obj.prBufferLength);
    std::swap(this->prBuffer, obj.prBuffer); 
    return *this;
}

The difference between the above move assignment operator and your move assignment operator is that the deallocation of memory is delayed while your version deallocates the memory right away, this might be important in some situations.

这篇关于std :: move里面的移动赋值运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆