You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

arithmetic.hpp 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. namespace ranges
  17. {
  18. /// \addtogroup group-functional
  19. /// @{
  20. struct plus
  21. {
  22. template<typename T, typename U>
  23. constexpr auto operator()(T && t, U && u) const -> decltype((T &&) t + (U &&) u)
  24. {
  25. return (T &&) t + (U &&) u;
  26. }
  27. using is_transparent = void;
  28. };
  29. struct minus
  30. {
  31. template<typename T, typename U>
  32. constexpr auto operator()(T && t, U && u) const -> decltype((T &&) t - (U &&) u)
  33. {
  34. return (T &&) t - (U &&) u;
  35. }
  36. using is_transparent = void;
  37. };
  38. struct multiplies
  39. {
  40. template<typename T, typename U>
  41. constexpr auto operator()(T && t, U && u) const -> decltype((T &&) t * (U &&) u)
  42. {
  43. return (T &&) t * (U &&) u;
  44. }
  45. using is_transparent = void;
  46. };
  47. struct bitwise_or
  48. {
  49. template<typename T, typename U>
  50. constexpr auto operator()(T && t, U && u) const -> decltype((T &&) t | (U &&) u)
  51. {
  52. return (T &&) t | (U &&) u;
  53. }
  54. using is_transparent = void;
  55. };
  56. template<typename T>
  57. struct convert_to
  58. {
  59. // clang-format off
  60. template<typename U>
  61. constexpr auto CPP_auto_fun(operator())(U &&u)(const)
  62. (
  63. return static_cast<T>((U &&) u)
  64. )
  65. // clang-format on
  66. };
  67. template<typename T>
  68. struct coerce
  69. {
  70. constexpr T & operator()(T & t) const
  71. {
  72. return t;
  73. }
  74. /// \overload
  75. constexpr T const & operator()(T const & t) const
  76. {
  77. return t;
  78. }
  79. /// \overload
  80. constexpr T operator()(T && t) const
  81. {
  82. return (T &&) t;
  83. }
  84. T operator()(T const &&) const = delete;
  85. };
  86. template<typename T>
  87. struct coerce<T const> : coerce<T>
  88. {};
  89. template<typename T>
  90. struct coerce<T &> : coerce<T>
  91. {};
  92. template<typename T>
  93. struct coerce<T &&> : coerce<T>
  94. {};
  95. /// @}
  96. } // namespace ranges
  97. #endif