Is this code correct?
Yes. C ++ 11 added for this reason std::vector::push_back(T&&).
Is there a better way to do this?
fn(const vector<int> &v) fn(vector<int> &&v) , v vectorOfVectors. fn , .
template<typename T>
void fn(T &&v) {
vectorOfVectors.push_back(std::forward<T>(v));
}
++ 11 std::forward. T vector<int>& , v lvalue, vector<int>&& , v r. , vector<int>& && vector<int>&, vector<int>&& && vector<int>&&. , , push_back, lvalue, , rvalue.
, , . ( "" g++ clang++). , , " ".
?
. , , , . , , .
, . , , Foo(const Foo&)=default, . explicit, "".