arithmetic.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*!
  2. @file
  3. Defines arithmetic operators.
  4. @copyright Louis Dionne 2013-2017
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  7. */
  8. #ifndef BOOST_HANA_DETAIL_OPERATORS_ARITHMETIC_HPP
  9. #define BOOST_HANA_DETAIL_OPERATORS_ARITHMETIC_HPP
  10. #include <boost/hana/config.hpp>
  11. #include <boost/hana/core/tag_of.hpp>
  12. #include <boost/hana/fwd/div.hpp>
  13. #include <boost/hana/fwd/minus.hpp>
  14. #include <boost/hana/fwd/mod.hpp>
  15. #include <boost/hana/fwd/mult.hpp>
  16. #include <boost/hana/fwd/negate.hpp>
  17. #include <boost/hana/fwd/plus.hpp>
  18. #include <type_traits>
  19. BOOST_HANA_NAMESPACE_BEGIN namespace detail {
  20. template <typename Tag>
  21. struct arithmetic_operators {
  22. static constexpr bool value = false;
  23. };
  24. namespace operators {
  25. template <typename X, typename Y, typename = typename std::enable_if<
  26. detail::arithmetic_operators<typename hana::tag_of<X>::type>::value ||
  27. detail::arithmetic_operators<typename hana::tag_of<Y>::type>::value
  28. >::type>
  29. constexpr auto operator+(X&& x, Y&& y)
  30. { return hana::plus(static_cast<X&&>(x), static_cast<Y&&>(y)); }
  31. template <typename X, typename Y, typename = typename std::enable_if<
  32. detail::arithmetic_operators<typename hana::tag_of<X>::type>::value ||
  33. detail::arithmetic_operators<typename hana::tag_of<Y>::type>::value
  34. >::type>
  35. constexpr auto operator-(X&& x, Y&& y)
  36. { return hana::minus(static_cast<X&&>(x), static_cast<Y&&>(y)); }
  37. template <typename X, typename = typename std::enable_if<
  38. detail::arithmetic_operators<typename hana::tag_of<X>::type>::value
  39. >::type>
  40. constexpr auto operator-(X&& x)
  41. { return hana::negate(static_cast<X&&>(x)); }
  42. template <typename X, typename Y, typename = typename std::enable_if<
  43. detail::arithmetic_operators<typename hana::tag_of<X>::type>::value ||
  44. detail::arithmetic_operators<typename hana::tag_of<Y>::type>::value
  45. >::type>
  46. constexpr auto operator*(X&& x, Y&& y)
  47. { return hana::mult(static_cast<X&&>(x), static_cast<Y&&>(y)); }
  48. template <typename X, typename Y, typename = typename std::enable_if<
  49. detail::arithmetic_operators<typename hana::tag_of<X>::type>::value ||
  50. detail::arithmetic_operators<typename hana::tag_of<Y>::type>::value
  51. >::type>
  52. constexpr auto operator/(X&& x, Y&& y)
  53. { return hana::div(static_cast<X&&>(x), static_cast<Y&&>(y)); }
  54. template <typename X, typename Y, typename = typename std::enable_if<
  55. detail::arithmetic_operators<typename hana::tag_of<X>::type>::value ||
  56. detail::arithmetic_operators<typename hana::tag_of<Y>::type>::value
  57. >::type>
  58. constexpr auto operator%(X&& x, Y&& y)
  59. { return hana::mod(static_cast<X&&>(x), static_cast<Y&&>(y)); }
  60. } // end namespace operators
  61. } BOOST_HANA_NAMESPACE_END
  62. #endif // !BOOST_HANA_DETAIL_OPERATORS_ARITHMETIC_HPP