假设我有一个类C
,唯一的目的是填写C
的某个类型的容器con_m
。因此,C
拥有以特定方式填写con_m
的方法。 C
已填满con
我不再需要C
,所以我想将con_n
转储到另一个变量中。但是C
实际上是一个仿函数,因为con_m
只能递增填充,所以不能是填充函数的局部对象。此外,实现应该隐藏给用户,所以他不需要在返回con_m
的函数上调用std::move
。是否使用std :: move来转储成员可接受的设计?
template <class container_type> class C {
public:
template <class T> void fill_some_more(const T &t) {
// do stuff with t filling con_m by another increment
}
container_type dump_container() { return std::move(con_m); }
container_type con_m;
};
int main() {
C<std::vector<int>> c;
while (some_condition) {
c.fill_some_more(some_int);
}
auto con = c.dump_container();
}
这是否适合使用std::move
?
听起来像'C'的方法可能更适合以容器作为参数的独立函数。如果首先不是会员,那么您不会遇到将集装箱从课堂中挖出的问题。 –
似乎取决于您的移动构造函数是否将源对象留在期望的状态 –
我会说不。通常移动构造函数不需要将容器留空。 – immibis