3 ms·
I’m not really a fan of the syntax in the article where you multiply the unit with the value. I much prefer your user defined literal approach. But is there a
by Negitivefrags 3y ago
I’m not really a fan of the syntax in the article where you multiply the unit with the value.
I much prefer your user defined literal approach.
But is there any way to make it so you can introduce a new user defined literal for a new composed unit with reasonable syntax?
- majorexception 3y agoSomething like this: auto operator"" _rpmPerVolt (long double x) { return decltype (1_rpm / 1_V)(x); } I have a macro for that: #define SI_DEFINE_LITERAL(xUnit, xliteral) \ [[nodiscard]] \ constexpr Quantity<units::xUnit> \ operator"" xliteral (long double value) \ { \ return Quantity<units::xUnit> (value); \ } \ \ [[nodiscard]] \ constexpr Quantity<units::xUnit> \ operator"" xliteral (unsigned long long value) \ { \ return Quantity<units::xUnit> (value); \ } // Base SI units: SI_DEFINE_LITERAL (Meter, _m) SI_DEFINE_LITERAL (Kilogram, _kg) SI_DEFINE_LITERAL (Second, _s) SI_DEFINE_LITERAL (Ampere, _A) SI_DEFINE_LITERAL (Kelvin, _K) SI_DEFINE_LITERAL (Mole, _mol) SI_DEFINE_LITERAL (Candela, _cd) SI_DEFINE_LITERAL (Radian, _rad) ...and a loong list of other common units here. The additional types are defined like this, in another file: using Foot = ScaledUnit<Meter, std::ratio<1'200, 3'937>>; using Mile = ScaledUnit<Meter, std::ratio<1'609'344, 1'000>>; using NauticalMile = ScaledUnit<Meter, std::ratio<1'852, 1>>; using Inch = ScaledUnit<Meter, std::ratio<254, 10'000>>; And then I use SI_DEFINE_LITERAL (NauticalMile, _nm); etc.