Skip to content

Commit c22caa2

Browse files
Merge pull request #12 from contour-terminal/improvement/left_operators
Add left operators (T,boxed<T,U>)
2 parents b43a20b + da83c85 commit c22caa2

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
@@ -112,6 +112,11 @@ template <typename T, typename U> constexpr boxed<T, U> operator-(boxed<T, U> co
112112
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}; }
113113
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}; }
114114

115+
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}; }
116+
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}; }
117+
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}; }
118+
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}; }
119+
115120
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; }
116121
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; }
117122
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
@@ -91,11 +91,15 @@ TEST_CASE("cast inside rvalue")
9191
{
9292
auto speed_of_light = Speed(299792458.0);
9393

94-
auto distance_auto = speed_of_light * 2.0;
95-
static_assert(std::is_same_v<decltype(distance_auto),Speed>);
96-
97-
double distance_d = speed_of_light * 2.0;
98-
REQUIRE(distance_d - 2.0 * 299792458.0 < std::numeric_limits<double>::epsilon());
94+
auto distance_auto_right = speed_of_light * 2.0;
95+
auto distance_auto_left = 2.0 * speed_of_light;
96+
static_assert(std::is_same_v<decltype(distance_auto_right),Speed>);
97+
static_assert(std::is_same_v<decltype(distance_auto_left),Speed>);
98+
99+
double distance_d_right = speed_of_light * 2.0;
100+
double distance_d_left = 2.0 * speed_of_light;
101+
REQUIRE(distance_d_right - 2.0 * 299792458.0 < std::numeric_limits<double>::epsilon());
102+
REQUIRE(distance_d_left - 2.0 * 299792458.0 < std::numeric_limits<double>::epsilon());
99103
}
100104

101105
TEST_CASE("all options for unbox")

0 commit comments

Comments
 (0)