Will c++11 smartly do the move for my temporary variable? Or, how can I make sure it is a move instead of a copy?
It depends. This
vec.push_back(MyStruct());
will bind to
std::vector<MyStruct>::push_back(MyStruct&&);
but whether the rvalue passed is moved or copied depends fully on whether MyStruct
has a move copy constructor (likewise for move assignment).
It will make absolutely no difference if you call
vec.push_back(std::move(MyStruct()));
because MyStruct()
is already an rvalue.
So it really depends on the details of MyStruct
. There is simply not enough information in your question to know if your class has move constructor.
These are the conditions that must be met for a class to have an implicitly generated move constructor:
- no user-declared copy constructors
- no user-declared copy assignment operators
- no user-declared move assignment operators
- no user-declared destructors
Of course, you can always provide your own if any of these conditions are not met:
MyStruct(MyStruct&&) = default;