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

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;
// 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
static_assert(is_of_type<42 * metre, quantity<metre, int>>);
static_assert(quantity<metre, int>::quantity_spec == length);