Skip to content

Commit e52305d

Browse files
committed
refactor: pow, sqrt, cbrt, square, and cubic made Hidden Friends for dimension, quantity_spec, reference, and unit
1 parent ae9d504 commit e52305d

File tree

4 files changed

+44
-142
lines changed

4 files changed

+44
-142
lines changed

src/core/include/mp-units/framework/dimension.h

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ struct dimension_interface {
8282
{
8383
return is_same_v<Lhs, Rhs>;
8484
}
85+
86+
template<std::intmax_t Num, std::intmax_t Den = 1, Dimension D>
87+
requires(Den != 0)
88+
[[nodiscard]] friend consteval Dimension auto pow(D d)
89+
{
90+
return detail::expr_pow<Num, Den, derived_dimension, struct dimension_one>(d);
91+
}
92+
93+
[[nodiscard]] friend consteval Dimension auto sqrt(Dimension auto d) { return pow<1, 2>(d); }
94+
[[nodiscard]] friend consteval Dimension auto cbrt(Dimension auto d) { return pow<1, 3>(d); }
8595
};
8696

8797
} // namespace detail
@@ -180,41 +190,6 @@ MP_UNITS_EXPORT_BEGIN
180190

181191
[[nodiscard]] consteval Dimension auto inverse(Dimension auto d) { return dimension_one / d; }
182192

183-
/**
184-
* @brief Computes the value of a dimension raised to the `Num/Den` power
185-
*
186-
* @tparam Num Exponent numerator
187-
* @tparam Den Exponent denominator
188-
* @param d Dimension being the base of the operation
189-
*
190-
* @return Dimension The result of computation
191-
*/
192-
template<std::intmax_t Num, std::intmax_t Den = 1, Dimension D>
193-
requires(Den != 0)
194-
[[nodiscard]] consteval Dimension auto pow(D d)
195-
{
196-
return detail::expr_pow<Num, Den, derived_dimension, struct dimension_one>(d);
197-
}
198-
199-
/**
200-
* @brief Computes the square root of a dimension
201-
*
202-
* @param d Dimension being the base of the operation
203-
*
204-
* @return Dimension The result of computation
205-
*/
206-
[[nodiscard]] consteval Dimension auto sqrt(Dimension auto d) { return mp_units::pow<1, 2>(d); }
207-
208-
/**
209-
* @brief Computes the cubic root of a dimension
210-
*
211-
* @param d Dimension being the base of the operation
212-
*
213-
* @return Dimension The result of computation
214-
*/
215-
[[nodiscard]] consteval Dimension auto cbrt(Dimension auto d) { return mp_units::pow<1, 3>(d); }
216-
217-
218193
struct dimension_symbol_formatting {
219194
#if MP_UNITS_COMP_CLANG || MP_UNITS_COMP_MSVC
220195
// TODO prevents the deprecated usage in implicit copy constructor warning

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

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,18 @@ struct quantity_spec_interface_base {
206206
{
207207
return is_same_v<Lhs, Rhs>;
208208
}
209+
210+
template<std::intmax_t Num, std::intmax_t Den = 1, QuantitySpec Q>
211+
requires(Den != 0)
212+
[[nodiscard]] friend consteval QuantitySpec auto pow(Q q)
213+
{
214+
return detail::clone_kind_of<Q{}>(
215+
detail::expr_pow<Num, Den, derived_quantity_spec, struct dimensionless, detail::type_list_of_quantity_spec_less>(
216+
detail::remove_kind(q)));
217+
}
218+
219+
[[nodiscard]] friend consteval QuantitySpec auto sqrt(QuantitySpec auto q) { return pow<1, 2>(q); }
220+
[[nodiscard]] friend consteval QuantitySpec auto cbrt(QuantitySpec auto q) { return pow<1, 3>(q); }
209221
};
210222

211223
[[nodiscard]] consteval bool explicitly_convertible_to_dimensionless(QuantitySpec auto qs);
@@ -641,50 +653,7 @@ template<QuantitySpec Q>
641653

642654
} // namespace detail
643655

644-
MP_UNITS_EXPORT_BEGIN
645-
646-
[[nodiscard]] consteval QuantitySpec auto inverse(QuantitySpec auto q) { return dimensionless / q; }
647-
648-
649-
/**
650-
* @brief Computes the value of a quantity specification raised to the `Num/Den` power
651-
*
652-
* @tparam Num Exponent numerator
653-
* @tparam Den Exponent denominator
654-
* @param q Quantity specification being the base of the operation
655-
*
656-
* @return QuantitySpec The result of computation
657-
*/
658-
template<std::intmax_t Num, std::intmax_t Den = 1, QuantitySpec Q>
659-
requires(Den != 0)
660-
[[nodiscard]] consteval QuantitySpec auto pow(Q q)
661-
{
662-
return detail::clone_kind_of<Q{}>(
663-
detail::expr_pow<Num, Den, derived_quantity_spec, struct dimensionless, detail::type_list_of_quantity_spec_less>(
664-
detail::remove_kind(q)));
665-
}
666-
667-
668-
/**
669-
* @brief Computes the square root of a quantity specification
670-
*
671-
* @param q Quantity specification being the base of the operation
672-
*
673-
* @return QuantitySpec The result of computation
674-
*/
675-
[[nodiscard]] consteval QuantitySpec auto sqrt(QuantitySpec auto q) { return mp_units::pow<1, 2>(q); }
676-
677-
678-
/**
679-
* @brief Computes the cubic root of a quantity specification
680-
*
681-
* @param q Quantity specification being the base of the operation
682-
*
683-
* @return QuantitySpec The result of computation
684-
*/
685-
[[nodiscard]] consteval QuantitySpec auto cbrt(QuantitySpec auto q) { return mp_units::pow<1, 3>(q); }
686-
687-
MP_UNITS_EXPORT_END
656+
MP_UNITS_EXPORT [[nodiscard]] consteval QuantitySpec auto inverse(QuantitySpec auto q) { return dimensionless / q; }
688657

689658
namespace detail {
690659

src/core/include/mp-units/framework/reference.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ struct reference {
132132
return {};
133133
}
134134

135-
[[nodiscard]] friend consteval detail::reference_t<MP_UNITS_EXPRESSION_WORKAROUND(mp_units::inverse(Q{})),
136-
MP_UNITS_EXPRESSION_WORKAROUND(mp_units::inverse(U{}))>
135+
[[nodiscard]] friend consteval detail::reference_t<MP_UNITS_EXPRESSION_WORKAROUND(inverse(Q{})),
136+
MP_UNITS_EXPRESSION_WORKAROUND(inverse(U{}))>
137137
inverse(reference)
138138
{
139139
return {};
@@ -150,8 +150,8 @@ struct reference {
150150
*/
151151
template<std::intmax_t Num, std::intmax_t Den = 1>
152152
requires(Den != 0)
153-
[[nodiscard]] friend consteval detail::reference_t<MP_UNITS_EXPRESSION_WORKAROUND((mp_units::pow<Num, Den>(Q{}))),
154-
MP_UNITS_EXPRESSION_WORKAROUND((mp_units::pow<Num, Den>(U{})))>
153+
[[nodiscard]] friend consteval detail::reference_t<MP_UNITS_EXPRESSION_WORKAROUND((pow<Num, Den>(Q{}))),
154+
MP_UNITS_EXPRESSION_WORKAROUND((pow<Num, Den>(U{})))>
155155
pow(reference)
156156
{
157157
return {};
@@ -164,8 +164,8 @@ struct reference {
164164
*
165165
* @return The result of computation
166166
*/
167-
[[nodiscard]] friend consteval detail::reference_t<MP_UNITS_EXPRESSION_WORKAROUND(mp_units::sqrt(Q{})),
168-
MP_UNITS_EXPRESSION_WORKAROUND(mp_units::sqrt(U{}))>
167+
[[nodiscard]] friend consteval detail::reference_t<MP_UNITS_EXPRESSION_WORKAROUND(sqrt(Q{})),
168+
MP_UNITS_EXPRESSION_WORKAROUND(sqrt(U{}))>
169169
sqrt(reference)
170170
{
171171
return {};
@@ -178,8 +178,8 @@ struct reference {
178178
*
179179
* @return The result of computation
180180
*/
181-
[[nodiscard]] friend consteval detail::reference_t<MP_UNITS_EXPRESSION_WORKAROUND(mp_units::cbrt(Q{})),
182-
MP_UNITS_EXPRESSION_WORKAROUND(mp_units::cbrt(U{}))>
181+
[[nodiscard]] friend consteval detail::reference_t<MP_UNITS_EXPRESSION_WORKAROUND(cbrt(Q{})),
182+
MP_UNITS_EXPRESSION_WORKAROUND(cbrt(U{}))>
183183
cbrt(reference)
184184
{
185185
return {};

src/core/include/mp-units/framework/unit.h

Lines changed: 13 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ template<detail::SymbolicConstant... Expr>
147147
struct derived_unit;
148148

149149
MP_UNITS_EXPORT struct one;
150-
MP_UNITS_EXPORT [[nodiscard]] MP_UNITS_CONSTEVAL Unit auto inverse(Unit auto u);
151150

152151
namespace detail {
153152

@@ -182,7 +181,7 @@ struct unit_interface {
182181
template<UnitMagnitude M, Unit U>
183182
[[nodiscard]] friend MP_UNITS_CONSTEVAL Unit auto operator/(M mag, U u)
184183
{
185-
return mag * mp_units::inverse(u);
184+
return mag * inverse(u);
186185
}
187186

188187
/**
@@ -236,6 +235,18 @@ struct unit_interface {
236235
{
237236
return kind_of<detail::get_associated_quantity(U{})>;
238237
}
238+
239+
template<std::intmax_t Num, std::intmax_t Den = 1, Unit U>
240+
requires(Den != 0)
241+
[[nodiscard]] friend consteval Unit auto pow(U u)
242+
{
243+
return detail::expr_pow<Num, Den, derived_unit, struct one>(u);
244+
}
245+
246+
[[nodiscard]] friend consteval Unit auto sqrt(Unit auto u) { return pow<1, 2>(u); }
247+
[[nodiscard]] friend consteval Unit auto cbrt(Unit auto u) { return pow<1, 3>(u); }
248+
[[nodiscard]] friend consteval Unit auto square(Unit auto u) { return pow<2>(u); }
249+
[[nodiscard]] friend consteval Unit auto cubic(Unit auto u) { return pow<3>(u); }
239250
};
240251

241252
template<Unit U, bool = requires { U::_point_origin_; }>
@@ -627,59 +638,6 @@ MP_UNITS_EXPORT_BEGIN
627638

628639
[[nodiscard]] MP_UNITS_CONSTEVAL Unit auto inverse(Unit auto u) { return one / u; }
629640

630-
/**
631-
* @brief Computes the value of a unit raised to the `Num/Den` power
632-
*
633-
* @tparam Num Exponent numerator
634-
* @tparam Den Exponent denominator
635-
* @param u Unit being the base of the operation
636-
*
637-
* @return Unit The result of computation
638-
*/
639-
template<std::intmax_t Num, std::intmax_t Den = 1, Unit U>
640-
requires(Den != 0)
641-
[[nodiscard]] consteval Unit auto pow(U u)
642-
{
643-
return detail::expr_pow<Num, Den, derived_unit, struct one>(u);
644-
}
645-
646-
/**
647-
* @brief Computes the square root of a unit
648-
*
649-
* @param u Unit being the base of the operation
650-
*
651-
* @return Unit The result of computation
652-
*/
653-
[[nodiscard]] consteval Unit auto sqrt(Unit auto u) { return mp_units::pow<1, 2>(u); }
654-
655-
/**
656-
* @brief Computes the cubic root of a unit
657-
*
658-
* @param u Unit being the base of the operation
659-
*
660-
* @return Unit The result of computation
661-
*/
662-
[[nodiscard]] consteval Unit auto cbrt(Unit auto u) { return mp_units::pow<1, 3>(u); }
663-
664-
/**
665-
* @brief Computes the square power of a unit
666-
*
667-
* @param u Unit being the base of the operation
668-
*
669-
* @return Unit The result of computation
670-
*/
671-
[[nodiscard]] consteval Unit auto square(Unit auto u) { return mp_units::pow<2>(u); }
672-
673-
/**
674-
* @brief Computes the cubic power of a unit
675-
*
676-
* @param u Unit being the base of the operation
677-
*
678-
* @return Unit The result of computation
679-
*/
680-
[[nodiscard]] consteval Unit auto cubic(Unit auto u) { return mp_units::pow<3>(u); }
681-
682-
683641
// common dimensionless units
684642
// clang-format off
685643
inline constexpr struct percent final : named_unit<"%", mag_ratio<1, 100> * one> {} percent;

0 commit comments

Comments
 (0)