From 66be3d7efc9b0a93802765a7a5a8c214e5c31351 Mon Sep 17 00:00:00 2001 From: Glen Fernandes Date: Mon, 2 Jun 2014 08:39:47 -0700 Subject: [PATCH] Migrate swap documentation to quickbook --- doc/core.qbk | 3 +- doc/swap.qbk | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++ index.html | 2 +- swap.html | 98 ---------------------------------------------------- 4 files changed, 100 insertions(+), 100 deletions(-) create mode 100644 doc/swap.qbk delete mode 100644 swap.html diff --git a/doc/core.qbk b/doc/core.qbk index 530541a..867055f 100644 --- a/doc/core.qbk +++ b/doc/core.qbk @@ -55,7 +55,7 @@ Currently, the Core library contains: [`boost::ref`] ] [ - [[@../../swap.html swap]] + [[link core.swap swap]] [`boost::swap`] ] [ @@ -78,3 +78,4 @@ Currently, the Core library contains: [include:core lightweight_test.qbk] [include:core no_exceptions_support.qbk] [include:core noncopyable.qbk] +[include:core swap.qbk] diff --git a/doc/swap.qbk b/doc/swap.qbk new file mode 100644 index 0000000..48b2ce4 --- /dev/null +++ b/doc/swap.qbk @@ -0,0 +1,97 @@ +[section:swap Header ] + +`template void swap(T& left, T& right);` + +[section Introduction] + +The template function `boost::swap` allows the values of two +variables to be swapped, using argument dependent lookup to +select a specialized swap function if available. If no +specialized swap function is available, `std::swap` is used. + +[endsect] + +[section Rationale] + +The generic `std::swap` function requires that the elements +to be swapped are assignable and copy constructible. It is +usually implemented using one copy construction and two +assignments - this is often both unnecessarily restrictive and +unnecessarily slow. In addition, where the generic swap +implementation provides only the basic guarantee, specialized +swap functions are often able to provide the no-throw exception +guarantee (and it is considered best practice to do so where +possible [footnote Scott Meyers, Effective C++ Third Edition, +Item 25: "Consider support for a non-throwing swap"]. + +The alternative to using argument dependent lookup in this +situation is to provide a template specialization of +`std::swap` for every type that requires a specialized swap. +Although this is legal C++, no Boost libraries use this method, +whereas many Boost libraries provide specialized swap functions +in their own namespaces. + +`boost::swap` also supports swapping built-in arrays. Note that +`std::swap` originally did not do so, but a request to add an +overload of `std::swap` for built-in arrays has been accepted +by the C++ Standards Committee[footnote + [@http://open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#809 + LWG Defect Report 809: std::swap should be overloaded for array + types]]. + +[endsect] + +[section Exception Safety] + +`boost::swap` provides the same exception guarantee as the +underlying swap function used, with one exception; for an array +of type `T[n]`, where `n > 1` and the underlying swap function +for `T` provides the strong exception guarantee, `boost::swap` +provides only the basic exception guarantee. + +[endsect] + +[section Requirements] + +Either: + +* T must be assignable +* T must be copy constructible + +Or: + +* A function with the signature `swap(T&,T&)` is available via + argument dependent lookup + +Or: + +* A template specialization of `std::swap` exists for T + +Or: + +* T is a built-in array of swappable elements + +[endsect] + +[section Portability] + +Several older compilers do not support argument dependent +lookup. On these compilers `boost::swap` will call +`std::swap`, ignoring any specialized swap functions that +could be found as a result of argument dependent lookup. + +[endsect] + +[section Credits] + +* *Niels Dekker* - for implementing and documenting support for + built-in arrays +* *Joseph Gauterin* - for the initial idea, implementation, + tests, and documentation +* *Steven Watanabe* - for the idea to make `boost::swap` less + specialized than `std::swap`, thereby allowing the function + to have the name 'swap' without introducing ambiguity + +[endsect] + +[endsect] diff --git a/index.html b/index.html index 9a29504..398f567 100644 --- a/index.html +++ b/index.html @@ -23,7 +23,7 @@

© 2014 Peter Dimov
© 2014 Glen Fernandes
- © 2013 Andrey Semashev + © 2014 Andrey Semashev

diff --git a/swap.html b/swap.html deleted file mode 100644 index 36f8425..0000000 --- a/swap.html +++ /dev/null @@ -1,98 +0,0 @@ - - - - - - Boost: Swap Documentation - - - -

- C++ Boost - Header <boost/swap.hpp> -

- -

Swap

- -

- template<class T> void swap(T& left, T& right); -

- - -

- The template function boost::swap allows the values of two variables to be swapped, using argument dependent lookup to select a specialized swap function if available. If no specialized swap function is available, std::swap is used. -

- - -

Rationale

-

- The generic std::swap function requires that the elements to be swapped are assignable and copy constructible. It is usually implemented using one copy construction and two assignments - this is often both unnecessarily restrictive and unnecessarily slow. In addition, where the generic swap implementation provides only the basic guarantee, specialized swap functions are often able to provide the no-throw exception guarantee (and it is considered best practice to do so where possible1).

-

- The alternative to using argument dependent lookup in this situation is to provide a template specialization of std::swap for every type that requires a specialized swap. Although this is legal C++, no Boost libraries use this method, whereas many Boost libraries provide specialized swap functions in their own namespaces. -

-

- boost::swap also supports swapping built-in arrays. Note that std::swap originally did not do so, but a request to add an overload of std::swap for built-in arrays has been accepted by the C++ Standards Committee2. -

- - -

Exception Safety

-

- boost::swap provides the same exception guarantee as the underlying swap function used, with one exception; for an array of type T[n], where n > 1 and the underlying swap function for T provides the strong exception guarantee, boost::swap provides only the basic exception guarantee. -

- - -

Requirements

-

Either:

-
    -
  • T must be assignable
  • -
  • T must be copy constructible
  • -
-

Or:

-
    -
  • A function with the signature swap(T&,T&) is available via argument dependent lookup
  • -
-

Or:

-
    -
  • A template specialization of std::swap exists for T
  • -
-

Or:

-
    -
  • T is a built-in array of swappable elements
  • -
- - - -

Portability

-

- Several older compilers do not support argument dependent lookup ‒ on these compilers boost::swap will call std::swap, ignoring any specialized swap functions that could be found as a result of argument dependent lookup. -

- - -

Credits

-
    -
  • - Niels Dekker - for implementing and documenting support for built-in arrays -
  • -
  • - Joseph Gauterin - for the initial idea, implementation, tests, and documentation -
  • -
  • - Steven Watanabe - for the idea to make boost::swap less specialized than std::swap, thereby allowing the function to have the name 'swap' without introducing ambiguity -
  • -
- - -
-

[1]Scott Meyers, Effective C++ Third Edition, Item 25: "Consider support for a non-throwing swap"

-

[2]LWG Defect Report 809 (std::swap should be overloaded for array types)

- - -
-

Revised: 08 September 2009

-

- Copyright 2007 - 2009 Joseph Gauterin. Use, modification, and distribution are subject to the Boost Software License, Version 1.0. - (See accompanying file LICENSE_1_0.txt or a copy at <http://www.boost.org/LICENSE_1_0.txt>.) -

- - -