我有一个实现了 move 语义的用户定义类(树结构)和一个 swap
功能。我想实现一个 move
以正确的方式运行,作为标准的 std::move 实现工作。
在树节点类中,每个子节点都有一个parent
指针,指向父节点。这意味着对于 move 操作,所有 child 都必须重新设置父级(并且可能有很多 child )
这意味着使用 swap
因为 move 不是最优的,因为两个列表的 child 在交换后必须重新设置。所以我想实现一个 move
清除移出树的函数。
std::move 实现的声明有些复杂,它们使用了 std::remove_reference<T>::type&&
返回类型。我需要这个吗?
最佳答案
您不需要编写 std::move 的特化。
如果您编写了正确的 move 构造函数和 move 赋值运算符,std::move 将适用于您的类。
例子:
#include <iostream>
#include <cstring>
using namespace std;
struct Thing {
Thing()
: _data(new int[100])
{
cout << "default construct\n";
}
// Copy operator
Thing(const Thing& other)
: _data(new int[100])
{
cout << "copy constructor\n";
memcpy(_data, other._data, sizeof(int) * 100);
}
// Move constructor
Thing(Thing&& other) noexcept
: _data(other._data)
{
cout << "move constructor\n";
other._data = nullptr;
}
// assignment operator
Thing& operator=(const Thing& rhs) {
cout << "copy operator\n";
if (&rhs != this) {
Thing tmp(rhs);
std::swap(*this, tmp);
}
return *this;
}
// move assignment operator
Thing& operator=(Thing&& rhs) noexcept {
cout << "move operator\n";
std::swap(_data, rhs._data);
return *this;
}
// destructor necessary since we are working in dangerous new/delete territory
~Thing() noexcept {
cout << "destructor " << (_data ? "object has data" : "object is empty") << "\n";
delete[] _data;
}
private:
int* _data;
};
int main()
{
cout << "constructing a\n";
Thing a;
cout << "constructing b with copy of a\n";
Thing b(a);
cout << "moving a to newly constructed c\n";
Thing c(std::move(a));
cout << "moving c back to a\n";
a = std::move(c);
cout << "create a new d\n";
Thing d;
cout << "replace d with a copy of a\n";
d = a;
return 0;
}
程序的输出:
constructing a
default construct
constructing b with copy of a
copy constructor
moving a to newly constructed c
move constructor
moving c back to a
move operator
create a new d
default construct
replace d with a copy of a
copy operator
copy constructor
move constructor
move operator
move operator
destructor object is empty
destructor object has data
destructor object has data
destructor object is empty
destructor object has data
destructor object has data
关于c++ - 如何为用户定义的类实现 c++11 move 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23604714/