Swapping like the standard

From the preceding review of how the standard library handles a swap, we can deduce the following guidelines:

  • Classes that support swap should implement swap() member functions that perform the operation in constant time
  • A free-standing swap() non-member function should also be provided for all types that can be swapped
  • Swapping two objects should not throw exceptions or otherwise fail

The latter guideline is less strict, and it is not always possible to follow it. In general, if the type has move operations that do not throw an exception, a non-throwing swap implementation is also possible. Note also that many exception-safety guarantees, and in particular those provided by the standard library, require that move and swap operations do not throw an exception.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.10.130