Index: boost/utility/swap.hpp =================================================================== --- boost/utility/swap.hpp (revision 46977) +++ boost/utility/swap.hpp (working copy) @@ -1,15 +1,19 @@ -// Copyright (C) 2007 Steven Watanabe, Joseph Gauterin +// Copyright (C) 2007, 2008 Steven Watanabe, Joseph Gauterin // // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // For more information, see http://www.boost.org +// +// Update: +// 29 June 2008 (Added support for built-in arrays.) Niels Dekker #ifndef BOOST_UTILITY_SWAP_HPP #define BOOST_UTILITY_SWAP_HPP #include //for std::swap +#include //for std::size_t namespace boost_swap_impl { @@ -19,6 +23,15 @@ using std::swap;//use std::swap if argument dependent lookup fails swap(left,right); } + + template + void swap_impl(T (& left)[N], T (& right)[N]) + { + for (std::size_t i = 0; i < N; ++i) + { + ::boost_swap_impl::swap_impl(left[i], right[i]); + } + } } namespace boost Index: libs/utility/swap.html =================================================================== --- libs/utility/swap.html (revision 46977) +++ libs/utility/swap.html (working copy) @@ -26,6 +26,10 @@

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 doesn't yet do so, but a request to add an overload of std::swap for built-in arrays has been well received by the Library Working Group of the C++ Standards Committee: LWG issue 809. std::swap should be overloaded for array types + boost::swap is no-throw for arrays whose element type has a no-throw swap. However, if the swap function of the element type provides the strong guarantee, then boost::swap only provides the basic guarantee, for arrays of size > 1. +

Requirements

@@ -42,6 +46,10 @@
  • A template specialization of std::swap exists for T
+

Or:

+
    +
  • T is a built-in array of swappable elements
  • +
@@ -56,6 +64,9 @@
  • Steven Wanatabe - for the idea to use a barrier namespaces, enabling the function to have the name 'swap' without introducing ambiguity or infinite recursion.
  • +
  • + Niels Dekker - for adding support for built-in arrays +
  • Joseph Gauterin - for the initial idea, final implementation, tests, and documentation.
  • @@ -63,7 +74,7 @@
    -

    Revised: 3rd October 2007

    +

    Revised: 29 June 2008

    Copyright 2007 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>.) Index: libs/utility/swap/test/Jamfile.v2 =================================================================== --- libs/utility/swap/test/Jamfile.v2 (revision 46977) +++ libs/utility/swap/test/Jamfile.v2 (working copy) @@ -1,4 +1,4 @@ -# Copyright (c) 2007 Joseph Gauterin +# Copyright (c) 2007, 2008 Joseph Gauterin # # Distributed under the Boost Software License, Version 1.0. # (See accompanying file LICENSE_1_0.txt or copy at @@ -20,6 +20,7 @@ [ run specialized_in_global.cpp ../../../test/build//boost_test_exec_monitor/static ] [ run specialized_in_other.cpp ../../../test/build//boost_test_exec_monitor/static ] [ run specialized_in_std.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run swap_arrays.cpp ../../../test/build//boost_test_exec_monitor/static ] ; Index: libs/utility/swap/test/swap_arrays.cpp =================================================================== --- libs/utility/swap/test/swap_arrays.cpp (revision 0) +++ libs/utility/swap/test/swap_arrays.cpp (revision 0) @@ -0,0 +1,39 @@ +// Copyright (c) 2008 Joseph Gauterin, Niels Dekker +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#include +#define BOOST_INCLUDE_MAIN +#include + +//Put test class in the global namespace +#include "./swap_test_class.hpp" + + +int test_main(int, char*[]) +{ + const std::size_t dimension = 7; + + swap_test_class array1[dimension]; + swap_test_class array2[dimension]; + boost::swap(array1, array2); + + BOOST_CHECK_EQUAL(swap_test_class::swap_count(), dimension); + BOOST_CHECK_EQUAL(swap_test_class::copy_count(), 0); + + swap_test_class::reset(); + + const std::size_t firstDimension = 3; + const std::size_t secondDimension = 4; + + swap_test_class two_d_array1[firstDimension][secondDimension]; + swap_test_class two_d_array2[firstDimension][secondDimension]; + boost::swap(two_d_array1, two_d_array1); + + BOOST_CHECK_EQUAL(swap_test_class::swap_count(), firstDimension*secondDimension); + BOOST_CHECK_EQUAL(swap_test_class::copy_count(), 0); + + return 0; +} Property changes on: libs\utility\swap\test\swap_arrays.cpp ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:keywords + Id Name: svn:eol-style + native