From 59ad95fb0fb374bd95da1f2e964a2ef5347052fa Mon Sep 17 00:00:00 2001 From: Chip Hogg Date: Mon, 11 Dec 2023 10:41:31 -0500 Subject: [PATCH] Let `u` in `unit_label(u)` be a unit slot (#206) I just noticed this wasn't already the case, and this seemed silly and" annoying. I also added a missing test case for the mechanism that makes unit slots work, namely, `AssociatedUnitT`. And I updated the docs. --- au/unit_of_measure.hh | 2 +- au/unit_of_measure_test.cc | 12 ++++++++++++ docs/reference/unit.md | 4 ++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/au/unit_of_measure.hh b/au/unit_of_measure.hh index 2755075f..62110c2b 100644 --- a/au/unit_of_measure.hh +++ b/au/unit_of_measure.hh @@ -793,7 +793,7 @@ constexpr template constexpr const auto &unit_label(Unit) { - return detail::as_char_array(UnitLabel::value); + return detail::as_char_array(UnitLabel>::value); } //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/au/unit_of_measure_test.cc b/au/unit_of_measure_test.cc index 5f03f164..e8430101 100644 --- a/au/unit_of_measure_test.cc +++ b/au/unit_of_measure_test.cc @@ -77,6 +77,12 @@ struct InvalidWrongMagType { using Mag = char; }; +// Useful for testing "unit slot" compatibility in APIs. +template +struct SomeUnitWrapper {}; +template +struct AssociatedUnit> : stdx::type_identity {}; + struct UnlabeledUnit : decltype(Feet{} * mag<9>()) {}; MATCHER_P(QuantityEquivalentToUnit, target, "") { @@ -218,6 +224,10 @@ TEST(AssociatedUnitT, IsIdentityForTypeWithNoAssociatedUnit) { StaticAssertTypeEq, double>(); } +TEST(AssociatedUnitT, HandlesWrappersWhichHaveSpecializedAssociatedUnit) { + StaticAssertTypeEq>, Feet>(); +} + TEST(UnitInverseT, CommutesWithProduct) { StaticAssertTypeEq>, UnitProductT, UnitInverseT>>(); @@ -598,6 +608,8 @@ TEST(UnitLabel, CommonPointUnitLabelWorksWithUnitProduct) { AnyOf(StrEq("COM_PT[m / min, in / min]"), StrEq("COM_PT[in / min, m / min]"))); } +TEST(UnitLabel, APICompatibleWithUnitSlots) { EXPECT_THAT(unit_label(feet), StrEq("ft")); } + namespace detail { TEST(Origin, ZeroForUnitWithNoSpecifiedOrigin) { diff --git a/docs/reference/unit.md b/docs/reference/unit.md index c1835532..7b5a1923 100644 --- a/docs/reference/unit.md +++ b/docs/reference/unit.md @@ -58,6 +58,10 @@ For a unit type `U`, or instance `u`, we can access the label as follows: - `unit_label()` - `unit_label(u)` +Note that the `u` in `unit_label(u)` is a [unit slot](../discussion/idioms/unit-slots.md), so you +can pass anything that "acts like a unit" to it. For instance, you can say `unit_label(meters)`; +you don't need to write `unit_label(Meters{})`. + This function returns a reference to the array, which again is a compile time constant. Note especially that the type is an _array_ (`[]`). A pointer (`*`) is _not_ acceptable. This is