简体   繁体   English

我可以在移动赋值运算符中调用析构函数吗?

[英]can I call destructor in move assignment operator?

Is calling a d-tor inside move assignment operator good practice? 在移动分配操作员里面调用一个d-tor好习惯吗?

here some example code: 这里有一些示例代码:

VectorList &operator = (VectorList &&other){
    ~VectorList(); // if this is not a good practice,
                   // I will need to paste whole d-tor here.

    _buffer     = std::move(other._buffer       );
    _dataCount  = std::move(other._dataCount    );
    _dataSize   = std::move(other._dataSize     );

    other._clear();

    return *this;
}

Should I use this code, or should I use swap() with move constructed object? 我应该使用此代码,还是应该使用swap()与移动构造对象?

~VectorList does more than run the code in the dtor body: it actually destroys the object. ~VectorList不仅运行dtor体中的代码:它实际上会破坏对象。

After that, the storage is unused. 之后,存储空间未使用。 You can construct a new object there using a constructor, but simply accessing members is going to either be undefined behaviour, or require language-lawyers to find the loophole that lets it be defined. 您可以使用构造函数在那里构造一个新对象,但只是访问成员将是未定义的行为,或者需要语言律师找到允许它被定义的漏洞。

Even if defined, it is dangerous, as an exception thrown while an automatic storage object is destroyed is bad news. 即使被定义,它也是危险的,因为在销毁自动存储对象时抛出的异常是坏消息。 Plus if the assigned-to class is actually of derived type, the dtor call itself is UB! 另外,如果指定的类实际上是派生类型,则dtor调用本身就是UB!

Neither is a worthwhile approach. 这两种方法都不值得。 The benefits are too small. 好处太小了。

The better alternative is copy-swap (which is at least easy to get correct: it can prevent some optimizations), or refactor out the 'clear' code from both the dtor and assignment. 更好的选择是copy-swap(至少很容易纠正:它可以阻止一些优化),或者从dtor和赋值中重构出“clear”代码。 Then call it at both spots. 然后在两个地方打电话给它。

Scott Meyers says don't use swap() : http://scottmeyers.blogspot.sg/2014/06/the-drawbacks-of-implementing-move.html Scott Meyers说不要使用swap()http//scottmeyers.blogspot.sg/2014/06/the-drawbacks-of-implementing-move.html

Regarding your current implementation, it seems you can do it more simply. 关于您当前的实施,似乎您可以更简单地做到这一点。 I imagine that the destructor actually deletes _buffer and does little else. 我想析构函数实际上删除了_buffer并且没有其他功能。 If that's true, you should just replace your harder-to-reason-about explicit destructor call with delete _buffer . 如果这是真的,你应该用delete _buffer替换你更难delete _buffer显式析构函数调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我可以使用删除的移动构造函数和赋值运算符移动 object? - Why can I move an object with deleted move constructor and assignment operator? 我可以在赋值运算符内部调用构造函数吗? - Can I call a Constructor inside of an assignment operator? 为什么定义析构函数会删除隐式定义的移动赋值运算符? - Why defining a destructor deletes the implicitly defined move assignment operator? 为什么默认赋值运算符不首先调用析构函数? - Why doesn't default assignment operator call the destructor first? 调用后的析构函数调用重载的赋值运算符-C ++ - Destructor call AFTER Overloaded Assignment Operator called - C++ 如何在我自己的类Vector中使用Move构造函数而不是Move赋值运算符? - How can I use Move constructor instead of Move assignment operator in my own class Vector? 在移动赋值运算符中使用std :: swap重用析构函数逻辑是否有意义? - Does it make sense to reuse destructor logic by using std::swap in a move assignment operator? 根据析构函数和移动构造函数实现移动赋值 - Implementing move assignment in terms of destructor and move constructor 为什么我要删除移动构造函数并在单例中移动赋值运算符? - Why should I delete move constructor and move assignment operator in a singleton? 移动赋值运算符和`if (this != &rhs)` - Move assignment operator and `if (this != &rhs)`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM