Why does push_back have two overloads for lvalues ​​and rvalues?

Why does a class / function have two overloads, one for lvalue and one for rvalue?

For example, this video says that we have two overloads for vector<T>::push_back

 void push_back( const T& value ); void push_back( T&& value ); 

Why can't we have only one overload by value,

 void push_back( T value ); 

If this value is lvalue, the value will be copied, and if this value is rvalue, the value will be moved. Isn't that how it works and is guaranteed by the standard?

+8
c ++ c ++ 11 move-semantics
source share
2 answers

With your proposal essentially, technically there will be copy + move or move + move, while with two other overloads there is one copy or one move.

+8
source share

Besides the issues mentioned above, this will also require a change to the old interface. And there are times when this is simply unacceptable.

+5
source share

All Articles