exchange.hpp 911 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. Copyright 2018 Glen Joseph Fernandes
  3. ([email protected])
  4. Distributed under the Boost Software License, Version 1.0.
  5. (http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #ifndef BOOST_CORE_EXCHANGE_HPP
  8. #define BOOST_CORE_EXCHANGE_HPP
  9. #include <boost/config.hpp>
  10. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  11. #include <boost/config/workaround.hpp>
  12. #include <utility>
  13. #endif
  14. namespace boost {
  15. #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  16. template<class T, class U>
  17. inline T exchange(T& t, const U& u)
  18. {
  19. T v = t;
  20. t = u;
  21. return v;
  22. }
  23. #else
  24. #if BOOST_WORKAROUND(BOOST_MSVC, < 1800)
  25. template<class T, class U>
  26. inline T exchange(T& t, U&& u)
  27. {
  28. T v = std::move(t);
  29. t = std::forward<U>(u);
  30. return v;
  31. }
  32. #else
  33. template<class T, class U = T>
  34. BOOST_CXX14_CONSTEXPR inline T exchange(T& t, U&& u)
  35. {
  36. T v = std::move(t);
  37. t = std::forward<U>(u);
  38. return v;
  39. }
  40. #endif
  41. #endif
  42. } /* boost */
  43. #endif