Will C ++ 11 do the move of my temporary variable? Or, how can I be sure that this will be a move instead of a copy?
It depends. it
vec.push_back(MyStruct());
binds to
std::vector<MyStruct>::push_back(MyStruct&&);
but the rvalue is transferred or copied, it completely depends on whether MyStruct has a move move constructor (similarly for assigning a move).
It won't make any difference if you call
vec.push_back(std::move(MyStruct()));
since MyStruct() already an rvalue.
So it really depends on the details of MyStruct . There is simply not enough information in your question to find out if your class has a move constructor.
These are the conditions that must be met in order for the class to have an implicitly generated move constructor:
- non-declared copy constructors
- unassigned copy assignment operators
- unassigned move assignment operators
- undeclared destructors
Of course, you can always provide your own if any of these conditions are not met:
MyStruct(MyStruct&&) = default;
source share