STD: swap is a template that provides a replacement operation that swaps the internal values of A and B of the same type. Swap operations require that objects support copying constructor and copying assignment function. In internal operations, three assignment objects are used.
There is a problem here, that is, for a specific object, its efficiency will be very low. Therefore, we need to make a special swap for a specific object.
Summary:
1,
If swap's default implementation code provides acceptable efficiency for your class or class template, you do not need to do anything else ,.
2,
If swap's default implementation version is inefficient, try to do the following:
(1)
Provides a public swap function to efficiently replace two objects of your type. This function cannot throw an exception. This restriction is limited to the member edition of swap.
(2)
In the namespace where your class or template is located, provide a non-member swap and allow it to call the swap member function.
(3)
If you are writing a class or template, feature STD: swap for your class and ask them to call your swap function.
3,
If you call swap, make sure that it contains a using declarative so that STD: swap can be exposed and visible in your function without any namespace modifier.
Note:
1,
When the efficiency of STD: swap is low, a swap member function is provided and the function does not throw an exception.
2,
If you provide a members swap, you should also declare a non-member swap to call the former. For Classes instead of template, you must also specialize in STD: swap.
3,
If you call swap, make sure that it contains a using declarative so that STD: swap can be exposed and visible in your function without any namespace modifier.
4,
It is good to fully specialize STD templates for user-defined types, but do not try to add something completely new to STD in STD.