Skip to content

Commit 49da1ce

Browse files
committed
refactor: make_reference added and used in constraints for quantity and quantity_point
`quantity_spec[unit]` syntax will not work for natural units and we want the interface to be widely applicable to all domains.
1 parent f9ffacc commit 49da1ce

File tree

3 files changed

+20
-19
lines changed

3 files changed

+20
-19
lines changed

src/core/include/mp-units/quantity.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,15 @@ class quantity {
143143

144144
// conversions
145145
template<Unit U>
146-
requires detail::QuantityConvertibleTo<quantity, quantity<quantity_spec[U{}], Rep>>
147-
[[nodiscard]] constexpr quantity<quantity_spec[U{}], Rep> in(U) const
146+
requires detail::QuantityConvertibleTo<quantity, quantity<detail::make_reference(quantity_spec, U{}), Rep>>
147+
[[nodiscard]] constexpr quantity<detail::make_reference(quantity_spec, U{}), Rep> in(U) const
148148
{
149-
return quantity<quantity_spec[U{}], Rep>{*this};
149+
return quantity<detail::make_reference(quantity_spec, U{}), Rep>{*this};
150150
}
151151

152152
template<Unit U>
153153
requires requires(quantity q) { value_cast<U{}>(q); }
154-
[[nodiscard]] constexpr quantity<quantity_spec[U{}], Rep> force_in(U) const
154+
[[nodiscard]] constexpr quantity<detail::make_reference(quantity_spec, U{}), Rep> force_in(U) const
155155
{
156156
return value_cast<U{}>(*this);
157157
}

src/core/include/mp-units/quantity_point.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,15 @@ class quantity_point {
169169
}
170170

171171
template<Unit U>
172-
requires detail::QuantityConvertibleTo<quantity_type, quantity<quantity_spec[U{}], Rep>>
173-
[[nodiscard]] constexpr quantity_point<quantity_spec[U{}], PO, Rep> in(U) const
172+
requires detail::QuantityConvertibleTo<quantity_type, quantity<detail::make_reference(quantity_spec, U{}), Rep>>
173+
[[nodiscard]] constexpr quantity_point<detail::make_reference(quantity_spec, U{}), PO, Rep> in(U) const
174174
{
175175
return make_quantity_point<PO>(quantity_ref_from(PO).in(U{}));
176176
}
177177

178178
template<Unit U>
179179
requires requires(quantity_type q) { value_cast<U{}>(q); }
180-
[[nodiscard]] constexpr quantity_point<quantity_spec[U{}], PO, Rep> force_in(U) const
180+
[[nodiscard]] constexpr quantity_point<detail::make_reference(quantity_spec, U{}), PO, Rep> force_in(U) const
181181
{
182182
return make_quantity_point<PO>(quantity_ref_from(PO).force_in(U{}));
183183
}

src/core/include/mp-units/quantity_spec.h

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ namespace mp_units {
3838

3939
namespace detail {
4040

41+
42+
template<QuantitySpec QS, Unit U>
43+
[[nodiscard]] consteval Reference auto make_reference(QS qs, U u)
44+
{
45+
if constexpr (detail::QuantityKindSpec<QS>)
46+
return u;
47+
else
48+
return reference<qs, u>{};
49+
}
50+
4151
// TODO revise the note in the below comment
4252
/**
4353
* @brief Returns the most restrictive character from the list
@@ -100,10 +110,7 @@ struct quantity_spec_interface {
100110
template<typename Self, UnitOf<Self{}> U>
101111
[[nodiscard]] consteval Reference auto operator[](this Self self, U u)
102112
{
103-
if constexpr (detail::QuantityKindSpec<Self>)
104-
return u;
105-
else
106-
return reference<self, u>{};
113+
return detail::make_reference(self, u);
107114
}
108115

109116
template<typename Self, typename Q>
@@ -117,10 +124,7 @@ struct quantity_spec_interface {
117124
template<typename Self_ = Self, UnitOf<Self_{}> U>
118125
[[nodiscard]] MP_UNITS_CONSTEVAL Reference auto operator[](U u) const
119126
{
120-
if constexpr (detail::QuantityKindSpec<Self_>)
121-
return u;
122-
else
123-
return reference<Self{}, u>{};
127+
return detail::make_reference(Self{}, u);
124128
}
125129

126130
template<typename Q, typename Self_ = Self>
@@ -296,10 +300,7 @@ struct quantity_spec<Self, QS, Args...> : std::remove_const_t<decltype(QS)> {
296300
template<typename Self_ = Self, UnitOf<Self_{}> U>
297301
[[nodiscard]] MP_UNITS_CONSTEVAL Reference auto operator[](U u) const
298302
{
299-
if constexpr (detail::QuantityKindSpec<Self>)
300-
return u;
301-
else
302-
return reference<Self{}, u>{};
303+
return detail::make_reference(Self{}, u);
303304
}
304305

305306
template<typename Q, typename Self_ = Self>

0 commit comments

Comments
 (0)