Skip to content

Commit

Permalink
fix: missing get_common_unit() overload added
Browse files Browse the repository at this point in the history
Resolves #666
  • Loading branch information
mpusz committed Jan 21, 2025
1 parent a489548 commit becb456
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/core/include/mp-units/framework/unit.h
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,16 @@ template<Unit... Us, Unit NewUnit>
return get_common_unit(cu, nu);
}

template<Unit Front, Unit... Rest, Unit... Us>
requires(interconvertible(common_unit<Front, Rest...>{}, common_unit<Us...>{}))
[[nodiscard]] consteval Unit auto get_common_unit(common_unit<Front, Rest...>, common_unit<Us...>)
{
if constexpr (sizeof...(Rest) == 1)
return get_common_unit(Front{}, get_common_unit(Rest{}..., common_unit<Us...>{}));
else
return get_common_unit(Front{}, get_common_unit(common_unit<Rest...>{}, common_unit<Us...>{}));
}

[[nodiscard]] consteval Unit auto get_common_unit(Unit auto u1, Unit auto u2, Unit auto u3, Unit auto... rest)
requires requires { get_common_unit(get_common_unit(u1, u2), u3, rest...); }
{
Expand Down
6 changes: 6 additions & 0 deletions test/static/unit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ inline constexpr struct pascal_ final : named_unit<"Pa", newton / square(metre)>
inline constexpr struct joule_ final : named_unit<"J", newton * metre> {} joule;
inline constexpr struct watt_ final : named_unit<"W", joule / second> {} watt;
inline constexpr struct degree_Celsius_ final : named_unit<symbol_text{u8"", "`C"}, kelvin> {} degree_Celsius;
inline constexpr struct degree_Fahrenheit_ final : named_unit<symbol_text{u8"", "`F"}, mag_ratio<5, 9> * degree_Celsius> {} degree_Fahrenheit;

inline constexpr struct minute_ final : named_unit<"min", mag<60> * second> {} minute;
inline constexpr struct hour_ final : named_unit<"h", mag<60> * minute> {} hour;
Expand Down Expand Up @@ -568,6 +569,11 @@ static_assert(is_of_type<get_common_unit(kilometre / hour, metre / second),
static_assert(is_of_type<get_common_unit(metre / second, kilometre / hour),
common_unit<decltype(kilometre / hour), decltype(metre / second)>>);
static_assert(is_of_type<get_common_unit(radian, degree), common_unit<degree_, radian_>>);
static_assert(
is_of_type<get_common_unit(degree_Celsius, degree_Fahrenheit), common_unit<degree_Celsius_, degree_Fahrenheit_>>);
static_assert(is_of_type<get_common_unit(get_common_unit(degree_Celsius, degree_Fahrenheit),
get_common_unit(mag_ratio<5, 9>* degree_Celsius, si::milli<kelvin>)),
common_unit<degree_Fahrenheit_, si::milli_<kelvin_>>>);

static_assert(
is_of_type<get_common_unit(mile, kilometre) / second, derived_unit<common_unit<kilo_<metre_>, mile_>, per<second_>>>);
Expand Down

0 comments on commit becb456

Please sign in to comment.