I'm curious as to the reasoning behind why std::ratio
only exposes the num
and den
values, and not some value
member which would represent the rational value of the ratio, possibly of the form
static constexpr double value = num / den;
I imagine it could be because
- They might not have wanted to lock in double (or any one particular) precision
- The operation is trivial to perform in user code
- It might require a nasty (unsafe?) cast
but I'm curious if there are other good reasons for the lack of a single value member in the standard?
ASSIDE:
There's a practical application to this for me, because I'm looking to implement a more strongly typed version of std::ratio
for a strongly-typed unit library that I've been working on, for which I think something like the following would make sense (assuming I'm not missing some understanding about std::ratio
and const-ness)
template<typename UnitType, std::intmax_t Num, std::intmax_t Denom = 1>
struct unit_ratio : private std::ratio<Num, Denom>
{
static const UnitType value() { return UnitType((double)num / den);
};
In this case, obviously performing num / den
in user code is not the same thing as being returned a UnitType
which contains the value num / den
, but I'm somewhat wary that it's a bad idea since std::ratio
doesn't do something similar.
Aucun commentaire:
Enregistrer un commentaire