1
0
forked from boostorg/core
Files
boost_core/include/boost/core/swap.hpp

83 lines
2.5 KiB
C++
Raw Normal View History

// Copyright (C) 2007, 2008 Steven Watanabe, 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)
// For more information, see http://www.boost.org
#ifndef BOOST_CORE_SWAP_HPP
#define BOOST_CORE_SWAP_HPP
// Note: the implementation of this utility contains various workarounds:
// - swap_impl is put outside the boost namespace, to avoid infinite
// recursion (causing stack overflow) when swapping objects of a primitive
// type.
// - swap_impl has a using-directive, rather than a using-declaration,
// because some compilers (including MSVC 7.1, Borland 5.9.3, and
// Intel 8.1) don't do argument-dependent lookup when it has a
// using-declaration instead.
// - boost::swap has two template arguments, instead of one, to
// avoid ambiguity when swapping objects of a Boost type that does
// not have its own boost::swap overload.
#include <boost/core/enable_if.hpp>
#include <boost/config.hpp>
#include <cstddef> //for std::size_t
// try to include std::swap from the most lightweight header
#if defined(BOOST_DINKUMWARE_STDLIB)
# include <utility>
#elif defined(_LIBCPP_VERSION) // since the first commit
# include <type_traits>
#elif defined(__GLIBCXX__) && __GLIBCXX__ > 20080621 // GCC 4.4
# include <bits/move.h>
#elif defined(__GLIBCXX__) && __GLIBCXX__ > 20071010 // GCC 4.3
# include <bits/stl_move.h>
#elif defined(__GLIBCXX__) || defined(__GLIBCPP__) // && __GLIBCPP__ > 20001005 // GCC 2.97
# include <bits/stl_algobase.h>
#elif __cplusplus >= 201103L
# include <utility>
#else // C++98/03 fallback
# include <algorithm>
#endif
namespace boost_swap_impl
{
// we can't use type_traits here
template<class T> struct is_const { enum _vt { value = 0 }; };
template<class T> struct is_const<T const> { enum _vt { value = 1 }; };
template<class T>
BOOST_GPU_ENABLED
void swap_impl(T& left, T& right)
{
using namespace std;//use std::swap if argument dependent lookup fails
swap(left,right);
}
template<class T, std::size_t N>
BOOST_GPU_ENABLED
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
{
template<class T1, class T2>
BOOST_GPU_ENABLED
typename enable_if_c< !boost_swap_impl::is_const<T1>::value && !boost_swap_impl::is_const<T2>::value >::type
swap(T1& left, T2& right)
{
::boost_swap_impl::swap_impl(left, right);
}
}
#endif