Browsing through some C++ questions I have often seen comments that a STL-friendly class should implement a swap
function (usually as a friend.) Can someone explain what benefits this brings, how the STL fits into this and why this function should be implemented as a friend
?
The benefit of a swap is that it helps investors to hedge their risk. Had the interest rates gone up to 8%, then Party A would be expected to pay party B a net of 2%. The downside of the swap contract is the investor could lose a lot of money.
Swap is used to give processes room, even when the physical RAM of the system is already used up. In a normal system configuration, when a system faces memory pressure, swap is used, and later when the memory pressure disappears and the system returns to normal operation, swap is no longer used.
Currency swap allows a customer to re-denominate a loan from one currency to another. ADVERTISEMENTS: The re-denomination from one currency to another currency is done to lower the borrowing cost for debt and to hedge exchange risk.
What are the benefits of interest rate swaps for borrowers? Swaps give the borrower flexibility - Separating the borrower's funding source from the interest rate risk allows the borrower to secure funding to meet its needs and gives the borrower the ability to create a swap structure to meet its specific goals.
For most classes, the default swap is fine, however, the default swap is not optimal in all cases. The most common example of this would be a class using the Pointer to Implementation idiom. Where as with the default swap a large amount of memory would get copied, is you specialized swap, you could speed it up significantly by only swapping the pointers.
If possible, it shouldn't be a friend of the class, however it may need to access private data (for example, the raw pointers) which you class probably doesn't want to expose in the class API.
The standard version of std::swap() will work for most types that are assignable.
void std::swap(T& lhs,T& rhs) { T tmp(lhs); lhs = rhs; rhs = tmp; }
But it is not an optimal implementation as it makes a call to the copy constructor followed by two calls to the assignment operator.
By adding your own version of std::swap() for your class you can implement an optimized version of swap().
For example std::vector. The default implementation as defined above would be very expensive as you would need to make copy of the whole data area. Potentially release old data areas or re-allocate the data area as well as invoke the copy constructor for the contained type on each item copied. A specialized version has a very simple easy way to do std::swap()
// NOTE this is not real code. // It is just an example to show how much more effecient swaping a vector could // be. And how using a temporary for the vector object is not required. std::swap(std::vector<T>& lhs,std::vector<T>& rhs) { std::swap(lhs.data,rhs.data); // swap a pointer to the data area std::swap(lhs.size,rhs.size); // swap a couple of integers with size info. std::swap(lhs.resv,rhs.resv); }
As a result if your class can optimize the swap() operation then you should probably do so. Otherwise the default version will be used.
Personally I like to implement swap() as a non throwing member method. Then provide a specialized version of std::swap():
class X { public: // As a side Note: // This is also useful for any non trivial class // Allows the implementation of the assignment operator // using the copy swap idiom. void swap(X& rhs) throw (); // No throw exception guarantee }; // Should be in the same namespace as X. // This will allows ADL to find the correct swap when used by objects/functions in // other namespaces. void swap(X& lhs,X& rhs) { lhs.swap(rhs); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With