Search code examples
c++11moveencapsulationrvalue-referencepush-back

R-value Reference push_back Function


I'm writing a Queue class. I have two versions of push_back for the new C++11 standard. One of these versions uses a rvalue reference as a parameter. My version works, but I think it must be lacking something:

 97 template <typename T>
 98 void Queue<T>::push( T && val ) 
 99 {
100     c.push_back( val );
101 }

It seems I should have used std::move, but I'm not sure how to implement this. c in the above function refers to a deque object encapsulated in my Queue class. Any help understanding what I should do to construct this function appropriately would be much appreciated!


Solution

  • Yes, you need std::move here, but you could also use emplace_back here:

    template <typename T>
    void Queue<T>::push( T && val ) 
    {
         c.emplace_back( std::move(val) );
    }