VC2013 移动运算符不递归执行移动
VC2013 move operator doesn't recursively perform move
以下代码无法在 VS2013 中编译。
#include <memory>
#include <vector>
struct Struct {
std::unique_ptr<int> data;
};
int main() {
std::vector<Struct> vec;
vec.emplace_back();
vec.emplace_back();
vec.front() = std::move(vec.back());
return 0;
}
我收到以下错误:
error C2280: attempting to reference a deleted function
似乎 VS 编译器试图在代码明确请求移动时调用赋值运算符。这是一个错误吗?这个问题有什么解决方法吗?
VS2013 不会自动生成所需的构造函数。
"Rvalue references v3.0" adds new rules to automatically generate move
constructors and move assignment operators under certain conditions.
However, this is not implemented in Visual C++ in Visual Studio 2013,
due to time and resource constraints.
所以为了编译程序,你必须至少实现这些构造函数:
struct Struct {
std::unique_ptr<int> data;
Struct() { }
// For exposition purposes only, change as needed
Struct(Struct&& o) : data(std::move(o.data)) {}
Struct& operator=(Struct&& other) {
data = std::move(other.data);
return *this;
}
};
以下代码无法在 VS2013 中编译。
#include <memory>
#include <vector>
struct Struct {
std::unique_ptr<int> data;
};
int main() {
std::vector<Struct> vec;
vec.emplace_back();
vec.emplace_back();
vec.front() = std::move(vec.back());
return 0;
}
我收到以下错误:
error C2280: attempting to reference a deleted function
似乎 VS 编译器试图在代码明确请求移动时调用赋值运算符。这是一个错误吗?这个问题有什么解决方法吗?
VS2013 不会自动生成所需的构造函数。
"Rvalue references v3.0" adds new rules to automatically generate move constructors and move assignment operators under certain conditions. However, this is not implemented in Visual C++ in Visual Studio 2013, due to time and resource constraints.
所以为了编译程序,你必须至少实现这些构造函数:
struct Struct {
std::unique_ptr<int> data;
Struct() { }
// For exposition purposes only, change as needed
Struct(Struct&& o) : data(std::move(o.data)) {}
Struct& operator=(Struct&& other) {
data = std::move(other.data);
return *this;
}
};