Skip to content

Commit c78f31f

Browse files
rothmichaelsclaude
andcommitted
fix: Enable runtime calls to mp_units::inverse() on Clang 18-20
Problem: Clang 18-20 strict immediate function enforcement prevents runtime calls to mp_units::inverse() due to internal consteval operators. Solution: Modified MP_UNITS_CONSTEVAL to use constexpr for Clang <21, consteval for Clang >=21. Preserves compile-time optimization while enabling runtime usage. Testing: Added comprehensive runtime tests for inverse() function covering time-to-frequency conversion, runtime parameters, and unit conversion. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 0955170 commit c78f31f

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/core/include/mp-units/bits/hacks.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ MP_UNITS_DIAGNOSTIC_POP
122122

123123
#endif
124124

125-
#if defined MP_UNITS_COMP_CLANG && MP_UNITS_COMP_CLANG < 17
125+
#if defined MP_UNITS_COMP_CLANG && MP_UNITS_COMP_CLANG < 21
126126

127127
#define MP_UNITS_CONSTEVAL constexpr
128128

test/runtime/math_test.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,4 +589,31 @@ TEST_CASE("math operations", "[math]")
589589
REQUIRE_THAT(atan2(1. * isq::length[km], 1000. * isq::length[m]), AlmostEquals(45. * angle[deg]));
590590
}
591591
}
592+
593+
SECTION("inverse functions")
594+
{
595+
SECTION("inverse of time quantity returns frequency")
596+
{
597+
auto period = 2.0 * isq::time[s];
598+
auto frequency = inverse<si::hertz>(period);
599+
REQUIRE(frequency == 0.5 * isq::frequency[Hz]);
600+
}
601+
602+
SECTION("inverse works with runtime values")
603+
{
604+
// Test the specific case that fails with consteval
605+
double runtime_value = 3.0;
606+
auto period = runtime_value * isq::time[s];
607+
auto frequency = inverse<si::hertz>(period);
608+
auto expected = (1.0/3.0) * isq::frequency[Hz];
609+
REQUIRE_THAT(frequency, AlmostEquals(expected));
610+
}
611+
612+
SECTION("inverse with different input units")
613+
{
614+
auto period_ms = 500.0 * isq::time[ms];
615+
auto frequency = inverse<si::hertz>(period_ms);
616+
REQUIRE(frequency == 2.0 * isq::frequency[Hz]);
617+
}
618+
}
592619
}

0 commit comments

Comments
 (0)