swap.hpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright (C) 2007, 2008 Steven Watanabe, Joseph Gauterin, Niels Dekker
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // For more information, see http://www.boost.org
  7. #ifndef BOOST_CORE_SWAP_HPP
  8. #define BOOST_CORE_SWAP_HPP
  9. // Note: the implementation of this utility contains various workarounds:
  10. // - swap_impl is put outside the boost namespace, to avoid infinite
  11. // recursion (causing stack overflow) when swapping objects of a primitive
  12. // type.
  13. // - swap_impl has a using-directive, rather than a using-declaration,
  14. // because some compilers (including MSVC 7.1, Borland 5.9.3, and
  15. // Intel 8.1) don't do argument-dependent lookup when it has a
  16. // using-declaration instead.
  17. // - boost::swap has two template arguments, instead of one, to
  18. // avoid ambiguity when swapping objects of a Boost type that does
  19. // not have its own boost::swap overload.
  20. #include <utility> //for std::swap (C++11)
  21. #include <algorithm> //for std::swap (C++98)
  22. #include <cstddef> //for std::size_t
  23. #include <boost/config.hpp>
  24. namespace boost_swap_impl
  25. {
  26. template<class T>
  27. BOOST_GPU_ENABLED
  28. void swap_impl(T& left, T& right)
  29. {
  30. using namespace std;//use std::swap if argument dependent lookup fails
  31. swap(left,right);
  32. }
  33. template<class T, std::size_t N>
  34. BOOST_GPU_ENABLED
  35. void swap_impl(T (& left)[N], T (& right)[N])
  36. {
  37. for (std::size_t i = 0; i < N; ++i)
  38. {
  39. ::boost_swap_impl::swap_impl(left[i], right[i]);
  40. }
  41. }
  42. }
  43. namespace boost
  44. {
  45. template<class T1, class T2>
  46. BOOST_GPU_ENABLED
  47. void swap(T1& left, T2& right)
  48. {
  49. ::boost_swap_impl::swap_impl(left, right);
  50. }
  51. }
  52. #endif