Skip to content

Commit da83c85

Browse files
committed
Add left operators (T,boxed<T,U>)
1 parent 02d2564 commit da83c85

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

include/boxed-cpp/boxed.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ template <typename T, typename U> constexpr boxed<T, U> operator-(boxed<T, U> co
124124
template <typename T, typename U> constexpr boxed<T, U> operator*(boxed<T, U> const& a, T b) noexcept { return boxed<T, U>{a.value * b}; }
125125
template <typename T, typename U> constexpr boxed<T, U> operator/(boxed<T, U> const& a, T b) noexcept { return boxed<T, U>{a.value / b}; }
126126

127+
template <typename T, typename U> constexpr boxed<T, U> operator+(T b, boxed<T, U> const& a) noexcept { return boxed<T, U>{b - a.value}; }
128+
template <typename T, typename U> constexpr boxed<T, U> operator-(T b, boxed<T, U> const& a) noexcept { return boxed<T, U>{b - a.value}; }
129+
template <typename T, typename U> constexpr boxed<T, U> operator*(T b, boxed<T, U> const& a) noexcept { return boxed<T, U>{b * a.value}; }
130+
template <typename T, typename U> constexpr boxed<T, U> operator/(T b, boxed<T, U> const& a) noexcept { return boxed<T, U>{b / a.value}; }
131+
127132
template <typename T, typename U> constexpr boxed<T, U>& operator+=(boxed<T, U>& a, boxed<T, U> const& b) noexcept { a.value += b.value; return a; }
128133
template <typename T, typename U> constexpr boxed<T, U>& operator-=(boxed<T, U>& a, boxed<T, U> const& b) noexcept { a.value -= b.value; return a; }
129134
template <typename T, typename U> constexpr boxed<T, U>& operator*=(boxed<T, U>& a, boxed<T, U> const& b) noexcept { a.value *= b.value; return a; }

test-boxed-cpp.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,15 @@ TEST_CASE("cast inside rvalue")
104104
{
105105
auto speed_of_light = Speed(299792458.0);
106106

107-
auto distance_auto = speed_of_light * 2.0;
108-
static_assert(std::is_same_v<decltype(distance_auto),Speed>);
109-
110-
double distance_d = speed_of_light * 2.0;
111-
REQUIRE(distance_d - 2.0 * 299792458.0 < std::numeric_limits<double>::epsilon());
107+
auto distance_auto_right = speed_of_light * 2.0;
108+
auto distance_auto_left = 2.0 * speed_of_light;
109+
static_assert(std::is_same_v<decltype(distance_auto_right),Speed>);
110+
static_assert(std::is_same_v<decltype(distance_auto_left),Speed>);
111+
112+
double distance_d_right = speed_of_light * 2.0;
113+
double distance_d_left = 2.0 * speed_of_light;
114+
REQUIRE(distance_d_right - 2.0 * 299792458.0 < std::numeric_limits<double>::epsilon());
115+
REQUIRE(distance_d_left - 2.0 * 299792458.0 < std::numeric_limits<double>::epsilon());
112116
}
113117

114118
TEST_CASE("all options for unbox")

0 commit comments

Comments
 (0)