I'm trying to efficiently pass a big vector to an object constructor, then empty it and finally re-use it. I wrote this solution:
template<typename T>
struct vect_obj{
vect_obj(vector<T> &&v) : v(move(v)) {}
vector<T> v;
};
int main()
{
vector<int> v(10,1);
//v.size()=10
vect_obj<int> v_o (move(v));
//v.size()=0
v.push_back(10);
//v.size()=1
}
But:
- Even if it works, I'm not sure if it's a good practice/correct way.
- Not sure if it's efficient (reason why I'm using move).