I have a class that has a member which is a vector of vectors. I would like to write a constructor for this class that takes an r-value reference to a single vector as an argument, and moves it into the member vector as a single element vector of the vector argument. So far I have:
class AClass
{
std::vector<std::vector<int>> member;
public:
AClass(std::vector<int> &&vec) : member(1)
{
member[0] = std::vector<int>(std::move(vec));
}
}
This seems to work correctly, but I am not sure if the std::move around vec is necessary. Or if the std::vector would have taken care of much of this for me had I written it a little differently.
It should be shorter to write:
in order to invoke
As far as I know, explicit moving is necessary, because
vecis not a rvalue (it is a named variable and can be used on the left side ofoperator=).