swap.qbk 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. [/
  2. / Copyright (c) 2008 Joseph Gauterin
  3. / Copyright (c) 2008, 2009 Niels Dekker
  4. / Copyright (c) 2014 Glen Fernandes
  5. /
  6. / Distributed under the Boost Software License, Version 1.0. (See
  7. / accompanying file LICENSE_1_0.txt or copy at
  8. / http://www.boost.org/LICENSE_1_0.txt)
  9. / For more information, see http://www.boost.org
  10. /]
  11. [section:swap swap]
  12. [simplesect Authors]
  13. * Niels Dekker
  14. * Joseph Gauterin
  15. * Steven Watanabe
  16. * Eric Niebler
  17. [endsimplesect]
  18. [section Header <boost/core/swap.hpp>]
  19. `template<class T> void swap(T& left, T& right);`
  20. [endsect]
  21. [section Introduction]
  22. The template function `boost::swap` allows the values of two
  23. variables to be swapped, using argument dependent lookup to
  24. select a specialized swap function if available. If no
  25. specialized swap function is available, `std::swap` is used.
  26. [endsect]
  27. [section Rationale]
  28. The generic `std::swap` function requires that the elements
  29. to be swapped are assignable and copy constructible. It is
  30. usually implemented using one copy construction and two
  31. assignments - this is often both unnecessarily restrictive and
  32. unnecessarily slow. In addition, where the generic swap
  33. implementation provides only the basic guarantee, specialized
  34. swap functions are often able to provide the no-throw exception
  35. guarantee (and it is considered best practice to do so where
  36. possible [footnote Scott Meyers, Effective C++ Third Edition,
  37. Item 25: "Consider support for a non-throwing swap"].
  38. The alternative to using argument dependent lookup in this
  39. situation is to provide a template specialization of
  40. `std::swap` for every type that requires a specialized swap.
  41. Although this is legal C++, no Boost libraries use this method,
  42. whereas many Boost libraries provide specialized swap functions
  43. in their own namespaces.
  44. `boost::swap` also supports swapping built-in arrays. Note that
  45. `std::swap` originally did not do so, but a request to add an
  46. overload of `std::swap` for built-in arrays has been accepted
  47. by the C++ Standards Committee[footnote
  48. [@http://open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#809
  49. LWG Defect Report 809: std::swap should be overloaded for array
  50. types]].
  51. [endsect]
  52. [section Exception Safety]
  53. `boost::swap` provides the same exception guarantee as the
  54. underlying swap function used, with one exception; for an array
  55. of type `T[n]`, where `n > 1` and the underlying swap function
  56. for `T` provides the strong exception guarantee, `boost::swap`
  57. provides only the basic exception guarantee.
  58. [endsect]
  59. [section Requirements]
  60. Either:
  61. * T must be assignable
  62. * T must be copy constructible
  63. Or:
  64. * A function with the signature `swap(T&,T&)` is available via
  65. argument dependent lookup
  66. Or:
  67. * A template specialization of `std::swap` exists for T
  68. Or:
  69. * T is a built-in array of swappable elements
  70. [endsect]
  71. [section Portability]
  72. Several older compilers do not support argument dependent
  73. lookup. On these compilers `boost::swap` will call
  74. `std::swap`, ignoring any specialized swap functions that
  75. could be found as a result of argument dependent lookup.
  76. [endsect]
  77. [section Credits]
  78. * *Niels Dekker* - for implementing and documenting support for
  79. built-in arrays
  80. * *Joseph Gauterin* - for the initial idea, implementation,
  81. tests, and documentation
  82. * *Steven Watanabe* - for the idea to make `boost::swap` less
  83. specialized than `std::swap`, thereby allowing the function
  84. to have the name 'swap' without introducing ambiguity
  85. [endsect]
  86. [endsect]