Will returning a vector<vector <int> > from a function invoke any move constructors in C++11
c++, c++11, move-semantics
Solution
`return` statements are specifically covered by the standard to be automatically treated as a move. So yes, this will invoke the move constructor.
The letter of the law for this is C++11, `[class.copy]§31+32`:
31 When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object ... This elision of copy/move operations, called copy elision, is permitted in the following circumstances (which may be combined to eliminate multiple copies):
- in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object (other than a function or catch-clause parameter) with the same cvunqualified type as the function return type, the copy/move operation can be omitted by constructing the automatic object directly into the function's return value
- ...
32 When the criteria for elision of a copy operation are met or would be met save for the fact that the source object is a function parameter, and the object to be copied is designated by an lvalue, overload resolution to select the constructor for the copy is first performed as if the object were designated by an rvalue. If overload resolution fails, or if the type of the first parameter of the selected constructor is not an rvalue reference to the object’s type (possibly cv-qualified), overload resolution is performed again, considering the object as an lvalue. ...
(Emphasis mine)
Combined and applied to your case, this means that when returning a local variable from a function, a move is attempted first and only if that's not possible, a copy will be performed.
(And, as @BjornPollex points out, it's quite likely even the move will be elided)
Problem
In C++11 will returning a `vector<vector<int> >` from a function invoke any move constructors? Or would the below code just make another copy of the all the vectors and their elements? ``` vector< vector<int> > Func() { vector< vector<int> > vec; //vec is filled here return vec; } ``` For stl containers of simple types is there a general to find when a move constructor is used or copy is made when returning them as values from functions?