feat: numerical_value_ref_in disallowed for rvalues
This commit is contained in:
1 parent
6a1e600f65
commit
2e26eed59d
11 files changed
+254
-297
No files matched your search
@@ -224,9 +224,9 @@ struct quantity_formatter {
|
||||
const quantity_format_specs<CharT>& specs;
|
||||
Locale loc;
|
||||
|
||||
explicit quantity_formatter(OutputIt o, quantity<Reference, Rep> q, const quantity_format_specs<CharT>& fspecs,
|
||||
explicit quantity_formatter(OutputIt o, const quantity<Reference, Rep>& q, const quantity_format_specs<CharT>& fspecs,
|
||||
Locale lc) :
|
||||
out(o), val(std::move(q).numerical_value_ref_in(q.unit)), specs(fspecs), loc(std::move(lc))
|
||||
out(o), val(q.numerical_value_ref_in(q.unit)), specs(fspecs), loc(std::move(lc))
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -51,9 +51,9 @@ template<QuantitySpec auto ToQS, typename Q>
|
||||
{
|
||||
if constexpr (detail::QuantityKindSpec<std::remove_const_t<decltype(ToQS)>> &&
|
||||
AssociatedUnit<std::remove_const_t<decltype(Q::unit)>>)
|
||||
return make_quantity<Q::unit>(std::forward<Q>(q).numerical_value_ref_in(q.unit));
|
||||
return make_quantity<Q::unit>(std::forward<Q>(q).numerical_value_);
|
||||
else
|
||||
return make_quantity<reference<ToQS, Q::unit>{}>(std::forward<Q>(q).numerical_value_ref_in(q.unit));
|
||||
return make_quantity<reference<ToQS, Q::unit>{}>(std::forward<Q>(q).numerical_value_);
|
||||
}
|
||||
|
||||
} // namespace mp_units
|
||||
@@ -63,10 +63,10 @@ template<Quantity To, typename From>
|
||||
if constexpr (q_unit == To::unit) {
|
||||
// no scaling of the number needed
|
||||
return make_quantity<To::reference>(static_cast<MP_UNITS_TYPENAME To::rep>(
|
||||
std::forward<From>(q).numerical_value_ref_in(q_unit))); // this is the only (and recommended) way to do
|
||||
// a truncating conversion on a number, so we are
|
||||
// using static_cast to suppress all the compiler
|
||||
// warnings on conversions
|
||||
std::forward<From>(q).numerical_value_)); // this is the only (and recommended) way to do
|
||||
// a truncating conversion on a number, so we are
|
||||
// using static_cast to suppress all the compiler
|
||||
// warnings on conversions
|
||||
} else {
|
||||
// scale the number
|
||||
constexpr Magnitude auto c_mag = get_canonical_unit(q_unit).mag / get_canonical_unit(To::unit).mag;
|
||||
@@ -78,9 +78,8 @@ template<Quantity To, typename From>
|
||||
using multiplier_type =
|
||||
conditional<treat_as_floating_point<c_rep_type>, std::common_type_t<c_mag_type, long double>, c_mag_type>;
|
||||
constexpr auto val = [](Magnitude auto m) { return get_value<multiplier_type>(m); };
|
||||
return static_cast<MP_UNITS_TYPENAME To::rep>(
|
||||
static_cast<c_rep_type>(std::forward<From>(q).numerical_value_ref_in(q_unit)) * val(num) / val(den) *
|
||||
val(irr)) *
|
||||
return static_cast<MP_UNITS_TYPENAME To::rep>(static_cast<c_rep_type>(std::forward<From>(q).numerical_value_) *
|
||||
val(num) / val(den) * val(irr)) *
|
||||
To::reference;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,7 +126,7 @@ public:
|
||||
|
||||
template<detail::QuantityConvertibleTo<quantity> Q>
|
||||
constexpr explicit(!std::convertible_to<typename Q::rep, Rep>) quantity(const Q& q) :
|
||||
numerical_value_(detail::sudo_cast<quantity>(q).numerical_value_ref_in(unit))
|
||||
numerical_value_(detail::sudo_cast<quantity>(q).numerical_value_)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -157,52 +157,36 @@ public:
|
||||
}
|
||||
|
||||
// data access
|
||||
#ifdef __cpp_explicit_this_parameter
|
||||
template<typename Self, Unit U>
|
||||
requires(U{} == unit)
|
||||
[[nodiscard]] constexpr auto&& numerical_value_ref_in(this Self&& self, U) noexcept
|
||||
{
|
||||
return std::forward<Self>(self).numerical_value_;
|
||||
}
|
||||
#else
|
||||
template<Unit U>
|
||||
requires(U{} == unit)
|
||||
[[nodiscard]] constexpr rep& numerical_value_ref_in(U) & noexcept
|
||||
{
|
||||
return numerical_value_;
|
||||
}
|
||||
|
||||
template<Unit U>
|
||||
requires(U{} == unit)
|
||||
[[nodiscard]] constexpr const rep& numerical_value_ref_in(U) const& noexcept
|
||||
{
|
||||
return numerical_value_;
|
||||
}
|
||||
|
||||
template<Unit U>
|
||||
requires(U{} == unit)
|
||||
[[nodiscard]] constexpr rep&& numerical_value_ref_in(U) && noexcept
|
||||
{
|
||||
return std::move(numerical_value_);
|
||||
}
|
||||
template<Unit U>
|
||||
requires(U{} == unit)
|
||||
[[nodiscard]] constexpr const rep&& numerical_value_ref_in(U) const&& noexcept
|
||||
{
|
||||
return std::move(numerical_value_);
|
||||
}
|
||||
#endif
|
||||
constexpr const rep&& numerical_value_ref_in(U) const&& noexcept = delete;
|
||||
|
||||
template<Unit U>
|
||||
requires requires(quantity q) { q.in(U{}); }
|
||||
[[nodiscard]] constexpr rep numerical_value_in(U) const noexcept
|
||||
{
|
||||
return (*this).in(U{}).numerical_value_ref_in(U{});
|
||||
return (*this).in(U{}).numerical_value_;
|
||||
}
|
||||
|
||||
template<Unit U>
|
||||
requires requires(quantity q) { q.force_in(U{}); }
|
||||
[[nodiscard]] constexpr rep force_numerical_value_in(U) const noexcept
|
||||
{
|
||||
return (*this).force_in(U{}).numerical_value_ref_in(U{});
|
||||
return (*this).force_in(U{}).numerical_value_;
|
||||
}
|
||||
|
||||
// member unary operators
|
||||
@@ -213,7 +197,7 @@ public:
|
||||
} -> std::common_with<rep>;
|
||||
}
|
||||
{
|
||||
return make_quantity<reference>(+numerical_value_ref_in(unit));
|
||||
return make_quantity<reference>(+numerical_value_);
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Quantity auto operator-() const
|
||||
@@ -223,7 +207,7 @@ public:
|
||||
} -> std::common_with<rep>;
|
||||
}
|
||||
{
|
||||
return make_quantity<reference>(-numerical_value_ref_in(unit));
|
||||
return make_quantity<reference>(-numerical_value_);
|
||||
}
|
||||
|
||||
constexpr quantity& operator++()
|
||||
@@ -387,8 +371,7 @@ template<auto R1, typename Rep1, auto R2, typename Rep2>
|
||||
[[nodiscard]] constexpr Quantity auto operator+(const quantity<R1, Rep1>& lhs, const quantity<R2, Rep2>& rhs)
|
||||
{
|
||||
using ret = detail::common_quantity_for<std::plus<>, quantity<R1, Rep1>, quantity<R2, Rep2>>;
|
||||
return make_quantity<ret::reference>(ret(lhs).numerical_value_ref_in(ret::unit) +
|
||||
ret(rhs).numerical_value_ref_in(ret::unit));
|
||||
return make_quantity<ret::reference>(ret(lhs).numerical_value_ + ret(rhs).numerical_value_);
|
||||
}
|
||||
|
||||
template<auto R1, typename Rep1, auto R2, typename Rep2>
|
||||
@@ -396,8 +379,7 @@ template<auto R1, typename Rep1, auto R2, typename Rep2>
|
||||
[[nodiscard]] constexpr Quantity auto operator-(const quantity<R1, Rep1>& lhs, const quantity<R2, Rep2>& rhs)
|
||||
{
|
||||
using ret = detail::common_quantity_for<std::minus<>, quantity<R1, Rep1>, quantity<R2, Rep2>>;
|
||||
return make_quantity<ret::reference>(ret(lhs).numerical_value_ref_in(ret::unit) -
|
||||
ret(rhs).numerical_value_ref_in(ret::unit));
|
||||
return make_quantity<ret::reference>(ret(lhs).numerical_value_ - ret(rhs).numerical_value_);
|
||||
}
|
||||
|
||||
template<auto R1, typename Rep1, auto R2, typename Rep2>
|
||||
@@ -407,8 +389,7 @@ template<auto R1, typename Rep1, auto R2, typename Rep2>
|
||||
{
|
||||
gsl_ExpectsAudit(rhs != rhs.zero());
|
||||
using ret = detail::common_quantity_for<std::modulus<>, quantity<R1, Rep1>, quantity<R2, Rep2>>;
|
||||
return make_quantity<ret::reference>(ret(lhs).numerical_value_ref_in(ret::unit) %
|
||||
ret(rhs).numerical_value_ref_in(ret::unit));
|
||||
return make_quantity<ret::reference>(ret(lhs).numerical_value_ % ret(rhs).numerical_value_);
|
||||
}
|
||||
|
||||
template<auto R1, typename Rep1, auto R2, typename Rep2>
|
||||
@@ -416,7 +397,7 @@ template<auto R1, typename Rep1, auto R2, typename Rep2>
|
||||
Rep2>
|
||||
[[nodiscard]] constexpr Quantity auto operator*(const quantity<R1, Rep1>& lhs, const quantity<R2, Rep2>& rhs)
|
||||
{
|
||||
return make_quantity<R1 * R2>(lhs.numerical_value_ref_in(get_unit(R1)) * rhs.numerical_value_ref_in(get_unit(R2)));
|
||||
return make_quantity<R1 * R2>(lhs.numerical_value_ * rhs.numerical_value_);
|
||||
}
|
||||
|
||||
template<auto R, typename Rep, typename Value>
|
||||
@@ -424,7 +405,7 @@ template<auto R, typename Rep, typename Value>
|
||||
detail::InvokeResultOf<get_quantity_spec(R).character, std::multiplies<>, Rep, const Value&>
|
||||
[[nodiscard]] constexpr Quantity auto operator*(const quantity<R, Rep>& q, const Value& v)
|
||||
{
|
||||
return make_quantity<R>(q.numerical_value_ref_in(get_unit(R)) * v);
|
||||
return make_quantity<R>(q.numerical_value_ * v);
|
||||
}
|
||||
|
||||
template<typename Value, auto R, typename Rep>
|
||||
@@ -432,7 +413,7 @@ template<typename Value, auto R, typename Rep>
|
||||
detail::InvokeResultOf<get_quantity_spec(R).character, std::multiplies<>, const Value&, Rep>
|
||||
[[nodiscard]] constexpr Quantity auto operator*(const Value& v, const quantity<R, Rep>& q)
|
||||
{
|
||||
return make_quantity<R>(v * q.numerical_value_ref_in(get_unit(R)));
|
||||
return make_quantity<R>(v * q.numerical_value_);
|
||||
}
|
||||
|
||||
template<auto R1, typename Rep1, auto R2, typename Rep2>
|
||||
@@ -440,7 +421,7 @@ template<auto R1, typename Rep1, auto R2, typename Rep2>
|
||||
[[nodiscard]] constexpr Quantity auto operator/(const quantity<R1, Rep1>& lhs, const quantity<R2, Rep2>& rhs)
|
||||
{
|
||||
gsl_ExpectsAudit(rhs != rhs.zero());
|
||||
return make_quantity<R1 / R2>(lhs.numerical_value_ref_in(get_unit(R1)) / rhs.numerical_value_ref_in(get_unit(R2)));
|
||||
return make_quantity<R1 / R2>(lhs.numerical_value_ / rhs.numerical_value_);
|
||||
}
|
||||
|
||||
template<auto R, typename Rep, typename Value>
|
||||
@@ -449,7 +430,7 @@ template<auto R, typename Rep, typename Value>
|
||||
[[nodiscard]] constexpr Quantity auto operator/(const quantity<R, Rep>& q, const Value& v)
|
||||
{
|
||||
gsl_ExpectsAudit(v != quantity_values<Value>::zero());
|
||||
return make_quantity<R>(q.numerical_value_ref_in(get_unit(R)) / v);
|
||||
return make_quantity<R>(q.numerical_value_ / v);
|
||||
}
|
||||
|
||||
template<typename Value, auto R, typename Rep>
|
||||
@@ -457,7 +438,7 @@ template<typename Value, auto R, typename Rep>
|
||||
detail::InvokeResultOf<get_quantity_spec(R).character, std::divides<>, const Value&, Rep>
|
||||
[[nodiscard]] constexpr Quantity auto operator/(const Value& v, const quantity<R, Rep>& q)
|
||||
{
|
||||
return make_quantity<::mp_units::one / R>(v / q.numerical_value_ref_in(get_unit(R)));
|
||||
return make_quantity<::mp_units::one / R>(v / q.numerical_value_);
|
||||
}
|
||||
|
||||
template<auto R1, typename Rep1, auto R2, typename Rep2>
|
||||
@@ -466,7 +447,7 @@ template<auto R1, typename Rep1, auto R2, typename Rep2>
|
||||
[[nodiscard]] constexpr bool operator==(const quantity<R1, Rep1>& lhs, const quantity<R2, Rep2>& rhs)
|
||||
{
|
||||
using ct = std::common_type_t<quantity<R1, Rep1>, quantity<R2, Rep2>>;
|
||||
return ct(lhs).numerical_value_ref_in(ct::unit) == ct(rhs).numerical_value_ref_in(ct::unit);
|
||||
return ct(lhs).numerical_value_ == ct(rhs).numerical_value_;
|
||||
}
|
||||
|
||||
template<auto R1, typename Rep1, auto R2, typename Rep2>
|
||||
@@ -475,7 +456,7 @@ template<auto R1, typename Rep1, auto R2, typename Rep2>
|
||||
[[nodiscard]] constexpr auto operator<=>(const quantity<R1, Rep1>& lhs, const quantity<R2, Rep2>& rhs)
|
||||
{
|
||||
using ct = std::common_type_t<quantity<R1, Rep1>, quantity<R2, Rep2>>;
|
||||
return ct(lhs).numerical_value_ref_in(ct::unit) <=> ct(rhs).numerical_value_ref_in(ct::unit);
|
||||
return ct(lhs).numerical_value_ <=> ct(rhs).numerical_value_;
|
||||
}
|
||||
|
||||
// make_quantity
|
||||
|
||||
@@ -111,8 +111,7 @@ struct quantity_spec_interface {
|
||||
requires Quantity<std::remove_cvref_t<Q>> &&
|
||||
(explicitly_convertible(std::remove_reference_t<Q>::quantity_spec, self))
|
||||
{
|
||||
return make_quantity<reference<self, std::remove_cvref_t<Q>::unit>{}>(
|
||||
std::forward<Q>(q).numerical_value_ref_in(q.unit));
|
||||
return make_quantity<reference<self, std::remove_cvref_t<Q>::unit>{}>(std::forward<Q>(q).numerical_value_);
|
||||
}
|
||||
#else
|
||||
template<typename Self_ = Self, UnitOf<Self_{}> U>
|
||||
@@ -129,8 +128,7 @@ struct quantity_spec_interface {
|
||||
(explicitly_convertible(std::remove_reference_t<Q>::quantity_spec, Self_{}))
|
||||
[[nodiscard]] constexpr Quantity auto operator()(Q&& q) const
|
||||
{
|
||||
return make_quantity<reference<Self{}, std::remove_cvref_t<Q>::unit>{}>(
|
||||
std::forward<Q>(q).numerical_value_ref_in(q.unit));
|
||||
return make_quantity<reference<Self{}, std::remove_cvref_t<Q>::unit>{}>(std::forward<Q>(q).numerical_value_);
|
||||
}
|
||||
#endif
|
||||
};
|
||||
@@ -309,8 +307,7 @@ struct quantity_spec<Self, QS, Args...> : std::remove_const_t<decltype(QS)> {
|
||||
(explicitly_convertible(std::remove_reference_t<Q>::quantity_spec, Self_{}))
|
||||
[[nodiscard]] constexpr Quantity auto operator()(Q&& q) const
|
||||
{
|
||||
return make_quantity<reference<Self{}, std::remove_cvref_t<Q>::unit>{}>(
|
||||
std::forward<Q>(q).numerical_value_ref_in(q.unit));
|
||||
return make_quantity<reference<Self{}, std::remove_cvref_t<Q>::unit>{}>(std::forward<Q>(q).numerical_value_);
|
||||
}
|
||||
#endif
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user