Skip to content

Commit

Permalink
[wpimath] Constexprify units unary operators
Browse files Browse the repository at this point in the history
  • Loading branch information
calcmogul committed Mar 29, 2022
1 parent 126e3de commit 947ed2d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
4 changes: 2 additions & 2 deletions wpimath/src/main/native/include/units/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -2328,7 +2328,7 @@ namespace units

// unary addition: +T
template<class Units, typename T, template<typename> class NonLinearScale>
inline unit_t<Units, T, NonLinearScale> operator+(const unit_t<Units, T, NonLinearScale>& u) noexcept
constexpr inline unit_t<Units, T, NonLinearScale> operator+(const unit_t<Units, T, NonLinearScale>& u) noexcept
{
return u;
}
Expand All @@ -2352,7 +2352,7 @@ namespace units

// unary addition: -T
template<class Units, typename T, template<typename> class NonLinearScale>
inline unit_t<Units, T, NonLinearScale> operator-(const unit_t<Units, T, NonLinearScale>& u) noexcept
constexpr inline unit_t<Units, T, NonLinearScale> operator-(const unit_t<Units, T, NonLinearScale>& u) noexcept
{
return unit_t<Units, T, NonLinearScale>(-u());
}
Expand Down
3 changes: 3 additions & 0 deletions wpimath/src/test/native/cpp/UnitsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3063,14 +3063,17 @@ TEST_F(Constexpr, construction) {
constexpr meter_t result0(0);
constexpr auto result1 = make_unit<meter_t>(1);
constexpr auto result2 = meter_t(2);
constexpr auto result3 = -3_m;

EXPECT_EQ(meter_t(0), result0);
EXPECT_EQ(meter_t(1), result1);
EXPECT_EQ(meter_t(2), result2);
EXPECT_EQ(meter_t(-3), result3);

EXPECT_TRUE(noexcept(result0));
EXPECT_TRUE(noexcept(result1));
EXPECT_TRUE(noexcept(result2));
EXPECT_TRUE(noexcept(result3));
}

TEST_F(Constexpr, constants) {
Expand Down

0 comments on commit 947ed2d

Please sign in to comment.