From 871769c815ed068665e94ad4cd403672855e7d4e Mon Sep 17 00:00:00 2001 From: Joseph Eng <91924258+KangarooKoala@users.noreply.github.com> Date: Thu, 2 Oct 2025 17:36:30 -0700 Subject: [PATCH] [wpimath] Fix units overload resolution (#8267) --- wpimath/src/main/native/include/units/base.h | 1 + wpimath/src/test/native/cpp/UnitsTest.cpp | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/wpimath/src/main/native/include/units/base.h b/wpimath/src/main/native/include/units/base.h index fc5ae4b29d..626d76e16e 100644 --- a/wpimath/src/main/native/include/units/base.h +++ b/wpimath/src/main/native/include/units/base.h @@ -1992,6 +1992,7 @@ namespace units * @param[in] rhs unit to copy. */ template class NlsRhs> + requires traits::is_unit_v && traits::is_unit_v && traits::is_convertible_unit_v constexpr unit_t(const unit_t& rhs) noexcept : nls(units::convert(rhs.m_value), std::true_type() /*store linear value*/) { diff --git a/wpimath/src/test/native/cpp/UnitsTest.cpp b/wpimath/src/test/native/cpp/UnitsTest.cpp index 6357e79103..3b6cd606e3 100644 --- a/wpimath/src/test/native/cpp/UnitsTest.cpp +++ b/wpimath/src/test/native/cpp/UnitsTest.cpp @@ -3513,3 +3513,18 @@ TEST_F(CaseStudies, pythagoreanTheorum) { pow<2>(RightTriangle::b::value()) == pow<2>(RightTriangle::c::value())); } + +TEST(Units, overloadResolution) { + // Slight hack to get nested functions + struct Scope { + static bool f(units::meter_t) { + return true; + }; + + static bool f(units::second_t) { + return false; + }; + }; + // Make sure this properly selects the meter overload + EXPECT_TRUE(Scope::f(1_mm)); +}