Ticket #2056: single_patch_of_boost_array_swap_modifications.patch
File single_patch_of_boost_array_swap_modifications.patch, 6.0 KB (added by , 14 years ago) |
---|
-
boost/utility/swap.hpp
1 // Copyright (C) 2007 Steven Watanabe, Joseph Gauterin1 // Copyright (C) 2007, 2008 Steven Watanabe, Joseph Gauterin 2 2 // 3 3 // Distributed under the Boost Software License, Version 1.0. (See 4 4 // accompanying file LICENSE_1_0.txt or copy at 5 5 // http://www.boost.org/LICENSE_1_0.txt) 6 6 // For more information, see http://www.boost.org 7 // 8 // Update: 9 // 29 June 2008 (Added support for built-in arrays.) Niels Dekker 7 10 8 11 9 12 #ifndef BOOST_UTILITY_SWAP_HPP 10 13 #define BOOST_UTILITY_SWAP_HPP 11 14 12 15 #include <algorithm> //for std::swap 16 #include <cstddef> //for std::size_t 13 17 14 18 namespace boost_swap_impl 15 19 { … … 19 23 using std::swap;//use std::swap if argument dependent lookup fails 20 24 swap(left,right); 21 25 } 26 27 template<class T, std::size_t N> 28 void swap_impl(T (& left)[N], T (& right)[N]) 29 { 30 for (std::size_t i = 0; i < N; ++i) 31 { 32 ::boost_swap_impl::swap_impl(left[i], right[i]); 33 } 34 } 22 35 } 23 36 24 37 namespace boost -
libs/utility/swap.html
26 26 <p> 27 27 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. 28 28 </p> 29 <p> 30 <tt>boost::swap</tt> also supports swapping built-in arrays. Note that <tt>std::swap</tt> doesn't yet do so, but a request to add an overload of <tt>std::swap</tt> for built-in arrays has been well received by the Library Working Group of the C++ Standards Committee: <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html#809">LWG issue 809. std::swap should be overloaded for array types</a> 31 <tt>boost::swap</tt> 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 <tt>boost::swap</tt> only provides the basic guarantee, for arrays of size > 1. 32 </p> 29 33 30 34 <!-- Requirements --> 31 35 <h2>Requirements</h2> … … 42 46 <ul> 43 47 <li>A template specialization of std::swap exists for T</li> 44 48 </ul> 49 <p>Or:</p> 50 <ul> 51 <li>T is a built-in array of swappable elements</li> 52 </ul> 45 53 46 54 47 55 <!-- Portability --> … … 56 64 <li> 57 65 <em>Steven Wanatabe</em> - for the idea to use a barrier namespaces, enabling the function to have the name '<tt>swap</tt>' without introducing ambiguity or infinite recursion. 58 66 </li> 67 <li> 68 <em>Niels Dekker</em> - for adding support for built-in arrays 69 </li> 59 70 <li> 60 71 <em><a href="mailto:Joseph.Gauterin@googlemail.com">Joseph Gauterin</a></em> - for the initial idea, final implementation, tests, and documentation. 61 72 </li> … … 63 74 64 75 <!-- Copyright info --> 65 76 <hr/> 66 <p>Revised: 3rd October 2007</p>77 <p>Revised: 29 June 2008</p> 67 78 <p> 68 79 Copyright 2007 Joseph Gauterin. Use, modification, and distribution are subject to the Boost Software License, Version 1.0. 69 80 (See accompanying file <a href="../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or a copy at <<a href="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>>.) -
libs/utility/swap/test/Jamfile.v2
1 # Copyright (c) 2007 Joseph Gauterin1 # Copyright (c) 2007, 2008 Joseph Gauterin 2 2 # 3 3 # Distributed under the Boost Software License, Version 1.0. 4 4 # (See accompanying file LICENSE_1_0.txt or copy at … … 20 20 [ run specialized_in_global.cpp ../../../test/build//boost_test_exec_monitor/<link>static ] 21 21 [ run specialized_in_other.cpp ../../../test/build//boost_test_exec_monitor/<link>static ] 22 22 [ run specialized_in_std.cpp ../../../test/build//boost_test_exec_monitor/<link>static ] 23 [ run swap_arrays.cpp ../../../test/build//boost_test_exec_monitor/<link>static ] 23 24 ; 24 25 25 26 -
libs/utility/swap/test/swap_arrays.cpp
1 // Copyright (c) 2008 Joseph Gauterin, Niels Dekker 2 // 3 // Distributed under the Boost Software License, Version 1.0. 4 // (See accompanying file LICENSE_1_0.txt or copy at 5 // http://www.boost.org/LICENSE_1_0.txt) 6 7 #include <boost/utility/swap.hpp> 8 #define BOOST_INCLUDE_MAIN 9 #include <boost/test/test_tools.hpp> 10 11 //Put test class in the global namespace 12 #include "./swap_test_class.hpp" 13 14 15 int test_main(int, char*[]) 16 { 17 const std::size_t dimension = 7; 18 19 swap_test_class array1[dimension]; 20 swap_test_class array2[dimension]; 21 boost::swap(array1, array2); 22 23 BOOST_CHECK_EQUAL(swap_test_class::swap_count(), dimension); 24 BOOST_CHECK_EQUAL(swap_test_class::copy_count(), 0); 25 26 swap_test_class::reset(); 27 28 const std::size_t firstDimension = 3; 29 const std::size_t secondDimension = 4; 30 31 swap_test_class two_d_array1[firstDimension][secondDimension]; 32 swap_test_class two_d_array2[firstDimension][secondDimension]; 33 boost::swap(two_d_array1, two_d_array1); 34 35 BOOST_CHECK_EQUAL(swap_test_class::swap_count(), firstDimension*secondDimension); 36 BOOST_CHECK_EQUAL(swap_test_class::copy_count(), 0); 37 38 return 0; 39 }