arithmetic.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /// \file
  2. // Range v3 library
  3. //
  4. // Copyright Eric Niebler 2013-present
  5. //
  6. // Use, modification and distribution is subject to the
  7. // Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. // Project home: https://github.com/ericniebler/range-v3
  12. //
  13. #ifndef RANGES_V3_FUNCTIONAL_ARITHMETIC_HPP
  14. #define RANGES_V3_FUNCTIONAL_ARITHMETIC_HPP
  15. #include <concepts/concepts.hpp>
  16. #include <range/v3/detail/prologue.hpp>
  17. namespace ranges
  18. {
  19. /// \addtogroup group-functional
  20. /// @{
  21. struct plus
  22. {
  23. template<typename T, typename U>
  24. constexpr auto operator()(T && t, U && u) const -> decltype((T &&) t + (U &&) u)
  25. {
  26. return (T &&) t + (U &&) u;
  27. }
  28. using is_transparent = void;
  29. };
  30. struct minus
  31. {
  32. template<typename T, typename U>
  33. constexpr auto operator()(T && t, U && u) const -> decltype((T &&) t - (U &&) u)
  34. {
  35. return (T &&) t - (U &&) u;
  36. }
  37. using is_transparent = void;
  38. };
  39. struct multiplies
  40. {
  41. template<typename T, typename U>
  42. constexpr auto operator()(T && t, U && u) const -> decltype((T &&) t * (U &&) u)
  43. {
  44. return (T &&) t * (U &&) u;
  45. }
  46. using is_transparent = void;
  47. };
  48. struct bitwise_or
  49. {
  50. template<typename T, typename U>
  51. constexpr auto operator()(T && t, U && u) const -> decltype((T &&) t | (U &&) u)
  52. {
  53. return (T &&) t | (U &&) u;
  54. }
  55. using is_transparent = void;
  56. };
  57. template<typename T>
  58. struct convert_to
  59. {
  60. // clang-format off
  61. template<typename U>
  62. constexpr auto CPP_auto_fun(operator())(U &&u)(const)
  63. (
  64. return static_cast<T>((U &&) u)
  65. )
  66. // clang-format on
  67. };
  68. template<typename T>
  69. struct coerce
  70. {
  71. constexpr T & operator()(T & t) const
  72. {
  73. return t;
  74. }
  75. /// \overload
  76. constexpr T const & operator()(T const & t) const
  77. {
  78. return t;
  79. }
  80. /// \overload
  81. constexpr T operator()(T && t) const
  82. {
  83. return (T &&) t;
  84. }
  85. T operator()(T const &&) const = delete;
  86. };
  87. template<typename T>
  88. struct coerce<T const> : coerce<T>
  89. {};
  90. template<typename T>
  91. struct coerce<T &> : coerce<T>
  92. {};
  93. template<typename T>
  94. struct coerce<T &&> : coerce<T>
  95. {};
  96. /// @}
  97. } // namespace ranges
  98. #include <range/v3/detail/epilogue.hpp>
  99. #endif