forked from mpusz/mp-units
feat: improve types readability by eliminating extraneous () for a value of a type for references
Resolves #533
This commit is contained in:
@@ -27,7 +27,7 @@
|
||||
|
||||
namespace mp_units {
|
||||
|
||||
template<QuantitySpec auto Q, Unit auto U>
|
||||
template<QuantitySpec Q, Unit U>
|
||||
struct reference;
|
||||
|
||||
namespace detail {
|
||||
@@ -36,7 +36,7 @@ namespace detail {
|
||||
template<typename T>
|
||||
struct is_specialization_of_reference : std::false_type {};
|
||||
|
||||
template<auto Q, auto U>
|
||||
template<typename Q, typename U>
|
||||
struct is_specialization_of_reference<reference<Q, U>> : std::true_type {};
|
||||
|
||||
} // namespace detail
|
||||
@@ -51,18 +51,18 @@ concept Reference = AssociatedUnit<T> || detail::is_specialization_of_reference<
|
||||
|
||||
[[nodiscard]] consteval QuantitySpec auto get_quantity_spec(AssociatedUnit auto u);
|
||||
|
||||
template<auto Q, auto U>
|
||||
template<typename Q, typename U>
|
||||
[[nodiscard]] consteval QuantitySpec auto get_quantity_spec(reference<Q, U>)
|
||||
{
|
||||
return Q;
|
||||
return Q{};
|
||||
}
|
||||
|
||||
[[nodiscard]] consteval Unit auto get_unit(AssociatedUnit auto u) { return u; }
|
||||
|
||||
template<auto Q, auto U>
|
||||
template<typename Q, typename U>
|
||||
[[nodiscard]] consteval Unit auto get_unit(reference<Q, U>)
|
||||
{
|
||||
return U;
|
||||
return U{};
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -47,7 +47,7 @@ template<QuantitySpec QS, Unit U>
|
||||
if constexpr (detail::QuantityKindSpec<QS>)
|
||||
return u;
|
||||
else
|
||||
return reference<QS{}, U{}>{};
|
||||
return reference<QS, U>{};
|
||||
}
|
||||
|
||||
// TODO revise the note in the below comment
|
||||
@@ -121,7 +121,7 @@ struct quantity_spec_interface {
|
||||
(explicitly_convertible(std::remove_reference_t<Q>::quantity_spec, self))
|
||||
{
|
||||
return quantity{std::forward<Q>(q).numerical_value_is_an_implementation_detail_,
|
||||
reference<self, std::remove_cvref_t<Q>::unit>{}};
|
||||
reference<Self, std::remove_const_t<decltype(std::remove_cvref_t<Q>::unit)>>{}};
|
||||
}
|
||||
#else
|
||||
template<typename Self_ = Self, UnitOf<Self_{}> U>
|
||||
@@ -136,7 +136,7 @@ struct quantity_spec_interface {
|
||||
[[nodiscard]] constexpr Quantity auto operator()(Q&& q) const
|
||||
{
|
||||
return quantity{std::forward<Q>(q).numerical_value_is_an_implementation_detail_,
|
||||
reference<Self{}, std::remove_cvref_t<Q>::unit>{}};
|
||||
reference<Self, std::remove_const_t<decltype(std::remove_cvref_t<Q>::unit)>>){}};
|
||||
}
|
||||
#endif
|
||||
};
|
||||
|
||||
@@ -53,22 +53,24 @@ namespace mp_units {
|
||||
* The following syntaxes are not allowed:
|
||||
* `2 / kmph`, `kmph * 3`, `kmph / 4`, `70 * isq::length[km] / isq:time[h]`.
|
||||
*/
|
||||
template<QuantitySpec auto Q, Unit auto U>
|
||||
template<QuantitySpec Q, Unit U>
|
||||
struct reference {
|
||||
template<auto Q2, auto U2>
|
||||
template<typename Q2, typename U2>
|
||||
[[nodiscard]] friend consteval bool operator==(reference, reference<Q2, U2>)
|
||||
{
|
||||
return Q == Q2 && U == U2;
|
||||
return Q{} == Q2{} && U{} == U2{};
|
||||
}
|
||||
|
||||
template<AssociatedUnit U2>
|
||||
[[nodiscard]] friend consteval bool operator==(reference, U2 u2)
|
||||
{
|
||||
return Q == get_quantity_spec(u2) && U == u2;
|
||||
return Q{} == get_quantity_spec(u2) && U{} == u2;
|
||||
}
|
||||
|
||||
template<auto Q2, auto U2>
|
||||
[[nodiscard]] friend consteval reference<Q * Q2, U * U2> operator*(reference, reference<Q2, U2>)
|
||||
template<typename Q2, typename U2>
|
||||
[[nodiscard]] friend consteval reference<std::remove_const_t<decltype(Q{} * Q2{})>,
|
||||
std::remove_const_t<decltype(U{} * U2{})>>
|
||||
operator*(reference, reference<Q2, U2>)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
@@ -77,7 +79,9 @@ struct reference {
|
||||
#if MP_UNITS_COMP_MSVC
|
||||
[[nodiscard]] friend consteval decltype(reference<Q * get_quantity_spec(U2{}), U * U2{}>{}) operator*(reference, U2)
|
||||
#else
|
||||
[[nodiscard]] friend consteval reference<Q * get_quantity_spec(U2{}), U * U2{}> operator*(reference, U2)
|
||||
[[nodiscard]] friend consteval reference<std::remove_const_t<decltype(Q{} * get_quantity_spec(U2{}))>,
|
||||
std::remove_const_t<decltype(U{} * U2{})>>
|
||||
operator*(reference, U2)
|
||||
#endif
|
||||
{
|
||||
return {};
|
||||
@@ -87,14 +91,18 @@ struct reference {
|
||||
#if MP_UNITS_COMP_MSVC
|
||||
[[nodiscard]] friend consteval decltype(reference<get_quantity_spec(U1{}) * Q, U1{} * U>{}) operator*(U1, reference)
|
||||
#else
|
||||
[[nodiscard]] friend consteval reference<get_quantity_spec(U1{}) * Q, U1{} * U> operator*(U1, reference)
|
||||
[[nodiscard]] friend consteval reference<std::remove_const_t<decltype(get_quantity_spec(U1{}) * Q{})>,
|
||||
std::remove_const_t<decltype(U1{} * U{})>>
|
||||
operator*(U1, reference)
|
||||
#endif
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
template<auto Q2, auto U2>
|
||||
[[nodiscard]] friend consteval reference<Q / Q2, U / U2> operator/(reference, reference<Q2, U2>)
|
||||
template<typename Q2, typename U2>
|
||||
[[nodiscard]] friend consteval reference<std::remove_const_t<decltype(Q{} / Q2{})>,
|
||||
std::remove_const_t<decltype(U{} / U2{})>>
|
||||
operator/(reference, reference<Q2, U2>)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
@@ -103,7 +111,9 @@ struct reference {
|
||||
#if MP_UNITS_COMP_MSVC
|
||||
[[nodiscard]] friend consteval decltype(reference<Q / get_quantity_spec(U2{}), U / U2{}>{}) operator/(reference, U2)
|
||||
#else
|
||||
[[nodiscard]] friend consteval reference<Q / get_quantity_spec(U2{}), U / U2{}> operator/(reference, U2)
|
||||
[[nodiscard]] friend consteval reference<std::remove_const_t<decltype(Q{} / get_quantity_spec(U2{}))>,
|
||||
std::remove_const_t<decltype(U{} / U2{})>>
|
||||
operator/(reference, U2)
|
||||
#endif
|
||||
{
|
||||
return {};
|
||||
@@ -113,13 +123,20 @@ struct reference {
|
||||
#if MP_UNITS_COMP_MSVC
|
||||
[[nodiscard]] friend consteval decltype(reference<get_quantity_spec(U1{}) / Q, U1{} / U>{}) operator/(U1, reference)
|
||||
#else
|
||||
[[nodiscard]] friend consteval reference<get_quantity_spec(U1{}) / Q, U1{} / U> operator/(U1, reference)
|
||||
[[nodiscard]] friend consteval reference<std::remove_const_t<decltype(get_quantity_spec(U1{}) / Q{})>,
|
||||
std::remove_const_t<decltype(U1{} / U{})>>
|
||||
operator/(U1, reference)
|
||||
#endif
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
[[nodiscard]] friend consteval reference<inverse(Q), inverse(U)> inverse(reference) { return {}; }
|
||||
[[nodiscard]] friend consteval reference<std::remove_const_t<decltype(inverse(Q{}))>,
|
||||
std::remove_const_t<decltype(inverse(U{}))>>
|
||||
inverse(reference)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Computes the value of a reference raised to the `Num/Den` power
|
||||
@@ -132,7 +149,9 @@ struct reference {
|
||||
*/
|
||||
template<std::intmax_t Num, std::intmax_t Den = 1>
|
||||
requires detail::non_zero<Den>
|
||||
[[nodiscard]] friend consteval reference<pow<Num, Den>(Q), pow<Num, Den>(U)> pow(reference)
|
||||
[[nodiscard]] friend consteval reference<std::remove_const_t<decltype(pow<Num, Den>(Q{}))>,
|
||||
std::remove_const_t<decltype(pow<Num, Den>(U{}))>>
|
||||
pow(reference)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
@@ -144,7 +163,12 @@ struct reference {
|
||||
*
|
||||
* @return The result of computation
|
||||
*/
|
||||
[[nodiscard]] friend consteval reference<sqrt(Q), sqrt(U)> sqrt(reference) { return {}; }
|
||||
[[nodiscard]] friend consteval reference<std::remove_const_t<decltype(sqrt(Q{}))>,
|
||||
std::remove_const_t<decltype(sqrt(U{}))>>
|
||||
sqrt(reference)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Computes the cubic root of a reference
|
||||
@@ -153,24 +177,29 @@ struct reference {
|
||||
*
|
||||
* @return The result of computation
|
||||
*/
|
||||
[[nodiscard]] friend consteval reference<cbrt(Q), cbrt(U)> cbrt(reference) { return {}; }
|
||||
[[nodiscard]] friend consteval reference<std::remove_const_t<decltype(cbrt(Q{}))>,
|
||||
std::remove_const_t<decltype(cbrt(U{}))>>
|
||||
cbrt(reference)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
template<auto Q2, auto U2>
|
||||
template<typename Q2, typename U2>
|
||||
[[nodiscard]] friend consteval bool convertible(reference, reference<Q2, U2>)
|
||||
{
|
||||
return implicitly_convertible(Q, Q2) && convertible(U, U2);
|
||||
return implicitly_convertible(Q{}, Q2{}) && convertible(U{}, U2{});
|
||||
}
|
||||
|
||||
template<AssociatedUnit U2>
|
||||
[[nodiscard]] friend consteval bool convertible(reference, U2 u2)
|
||||
{
|
||||
return implicitly_convertible(Q, get_quantity_spec(u2)) && convertible(U, u2);
|
||||
return implicitly_convertible(Q{}, get_quantity_spec(u2)) && convertible(U{}, u2);
|
||||
}
|
||||
|
||||
template<AssociatedUnit U1>
|
||||
[[nodiscard]] friend consteval bool convertible(U1 u1, reference)
|
||||
{
|
||||
return implicitly_convertible(get_quantity_spec(u1), Q) && convertible(u1, U);
|
||||
return implicitly_convertible(get_quantity_spec(u1), Q{}) && convertible(u1, U{});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -246,8 +275,9 @@ template<Reference R1, Reference R2, Reference... Rest>
|
||||
} -> Unit;
|
||||
}
|
||||
{
|
||||
return reference<common_quantity_spec(get_quantity_spec(r1), get_quantity_spec(r2), get_quantity_spec(rest)...),
|
||||
common_unit(get_unit(r1), get_unit(r2), get_unit(rest)...)>{};
|
||||
return reference<std::remove_const_t<decltype(common_quantity_spec(get_quantity_spec(r1), get_quantity_spec(r2),
|
||||
get_quantity_spec(rest)...))>,
|
||||
std::remove_const_t<decltype(common_unit(get_unit(r1), get_unit(r2), get_unit(rest)...))>>{};
|
||||
}
|
||||
|
||||
namespace detail {
|
||||
@@ -258,8 +288,8 @@ template<AssociatedUnit auto To, AssociatedUnit From>
|
||||
return {};
|
||||
}
|
||||
|
||||
template<Unit auto To, QuantitySpec auto QS, Unit auto U>
|
||||
[[nodiscard]] consteval reference<QS, To> clone_reference_with(reference<QS, U>)
|
||||
template<Unit auto To, QuantitySpec QS, Unit U>
|
||||
[[nodiscard]] consteval reference<QS, std::remove_const_t<decltype(To)>> clone_reference_with(reference<QS, U>)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
@@ -65,9 +65,9 @@ struct system_reference {
|
||||
template<Unit U>
|
||||
requires(convertible(coherent_unit, U{}))
|
||||
#if MP_UNITS_COMP_MSVC
|
||||
[[nodiscard]] constexpr decltype(reference<quantity_spec, U{}>{}) operator[](U) const
|
||||
[[nodiscard]] constexpr decltype(reference<std::remove_const_t<decltype(quantity_spec)>, U>{}) operator[](U) const
|
||||
#else
|
||||
[[nodiscard]] constexpr reference<quantity_spec, U{}> operator[](U) const
|
||||
[[nodiscard]] constexpr reference<std::remove_const_t<decltype(quantity_spec)>, U> operator[](U) const
|
||||
#endif
|
||||
{
|
||||
return {};
|
||||
|
||||
Reference in New Issue
Block a user