在 C++ 中将具有 unique_ptr 的对象推入向量 [英] Pushing an object with unique_ptr into vector in C++
问题描述
我有一个简单的类结构,对离散模拟进行建模,带有一个状态向量,每个状态包含许多转换,作为智能指针的向量.我使用智能指针来保存转换,因为在我的完整应用程序中我需要多态性.
I have a simple class structure modelling a discrete simulation, with a vector of States, which each contain a number of Transitions, held as a vector of smart pointers. I've used smart pointers to hold the transitions as in my full application I need polymorphism.
#include <vector>
#include <memory>
class Transition {
public:
Transition() {}
};
class State {
public:
State(int num) : num(num), transitions() {}
void add_transition(std::unique_ptr<Transition> trans) {
transitions.push_back(std::move(trans));
}
private:
int num;
std::vector<std::unique_ptr<Transition>> transitions;
};
int main() {
std::vector<State> states;
for (int i = 0; i < 10; i++) {
State nstate = State(i);
for (int j = 0; j < 2; j++) {
nstate.add_transition(std::move(std::unique_ptr<Transition>(new Transition())));
}
// This line causes compiler errors
states.push_back(nstate);
}
}
将新状态对象添加到向量时出现编译器错误:
I get compiler errors when adding the new state object to the vector:
Error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = Transition; _Dp = std::default_delete<Transition>]’
{ ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
我想这是由于向量制作了 State 对象的副本,该对象也试图制作 unique_ptrs
的向量的副本,这是不允许的.我已经看到 emplace_back
不像 push_back
那样制作副本,但我仍然遇到同样的错误.
I imagine this is due to vector making a copy of the State object which is also trying to make a copy of the vector of unique_ptrs
which isn't allowed. I've seen that emplace_back
doesn't make copies like push_back
does but I still get the same error.
将 State 对象直接添加到向量中是可行的,但我更愿意避免这种变通方法,因为在我的实际代码中,我对 State 对象做了更多的工作,而不仅仅是添加转换并且不想继续访问向量.
Adding the State object directly into the vector works, but I'd prefer to avoid this workaround as in my actual code I do more work with the State object rather than just adding transitions and don't want to keep accessing the vector.
int main() {
std::vector<State> states;
for (int i = 0; i < 10; i++) {
states.push_back(State(i));
for (int j = 0; j < 2; j++) {
states[i].add_transition(std::move(std::unique_ptr<Transition>(new Transition())));
}
}
}
推荐答案
State
不可复制,只能移动;但是对于 states.push_back(nstate);
,nstate
是一个左值(作为命名变量),不能从中移动.然后尝试执行副本,但不允许.
State
is not copyable, but only moveable; But for states.push_back(nstate);
, nstate
is an lvalue (as a named variable), which can't be moved from. Then a copy is tried to perform but it's not allowed.
要解决它,您可以使用 std::move
(将其转换为右值):
To solve it you could use std::move
(to turn it to an rvalue):
states.push_back(std::move(nstate));
注意,在移动操作之后,nstate
的数据成员(包括向量及其内容)也会被移动.
Note that after the move operation, the data member of nstate
(including the vector and its content) will be moved too.
这篇关于在 C++ 中将具有 unique_ptr 的对象推入向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!