feat: kind_of_qs[unit] now returns a bare unit as a reference

This commit is contained in:
Mateusz Pusz
2023-08-18 11:01:40 +02:00
parent a3cf3f4087
commit ef39b8c4c7
2 changed files with 19 additions and 3 deletions

View File

@ -104,7 +104,10 @@ struct quantity_spec_interface {
template<typename Self, UnitOf<Self{}> U> template<typename Self, UnitOf<Self{}> U>
[[nodiscard]] consteval Reference auto operator[](this Self self, U u) [[nodiscard]] consteval Reference auto operator[](this Self self, U u)
{ {
return reference<self, u>{}; if constexpr (detail::QuantityKindSpec<Self>)
return u;
else
return reference<self, u>{};
} }
template<typename Self, typename Q> template<typename Self, typename Q>
@ -118,7 +121,10 @@ struct quantity_spec_interface {
template<typename Self_ = Self, UnitOf<Self_{}> U> template<typename Self_ = Self, UnitOf<Self_{}> U>
[[nodiscard]] consteval Reference auto operator[](U u) const [[nodiscard]] consteval Reference auto operator[](U u) const
{ {
return reference<Self{}, u>{}; if constexpr (detail::QuantityKindSpec<Self_>)
return u;
else
return reference<Self{}, u>{};
} }
template<typename Q, typename Self_ = Self> template<typename Q, typename Self_ = Self>
@ -294,7 +300,10 @@ struct quantity_spec<Self, QS, Args...> : std::remove_const_t<decltype(QS)> {
template<typename Self_ = Self, UnitOf<Self_{}> U> template<typename Self_ = Self, UnitOf<Self_{}> U>
[[nodiscard]] consteval Reference auto operator[](U u) const [[nodiscard]] consteval Reference auto operator[](U u) const
{ {
return reference<Self{}, u>{}; if constexpr (detail::QuantityKindSpec<Self>)
return u;
else
return reference<Self{}, u>{};
} }
template<typename Q, typename Self_ = Self> template<typename Q, typename Self_ = Self>

View File

@ -105,6 +105,13 @@ inline constexpr struct kilometre_ : decltype(si::kilo<metre>) {} kilometre;
inline constexpr struct bit_ : named_unit<"bit", one, kind_of<storage_capacity>> {} bit; inline constexpr struct bit_ : named_unit<"bit", one, kind_of<storage_capacity>> {} bit;
// clang-format on // clang-format on
static_assert(is_of_type<length[metre], reference<length, metre>>);
static_assert(is_of_type<kind_of<length>[metre], metre_>);
static_assert(is_of_type<(length / time)[metre / second], reference<length / time, metre / second>>);
static_assert(is_of_type<(kind_of<length> / kind_of<time>)[metre / second], derived_unit<metre_, per<second_>>>);
// Unit as a reference // Unit as a reference
static_assert(is_of_type<42 * metre, quantity<metre, int>>); static_assert(is_of_type<42 * metre, quantity<metre, int>>);
static_assert(quantity<metre, int>::quantity_spec == length); static_assert(quantity<metre, int>::quantity_spec == length);