void increase_speed(double); Rectangle(int,int,int,int);
void increase_speed(Speed); // Rectangle(Point topLeft, BoxWH b); // , .
template<int M, int K, int S> class Unit { // () public: enum { m = M, kg = K, s = S }; };
template<typename Unit> struct Value { double val; // explicit Value(double d) : val(d) {} public: /* * - */ static constexpr int m() {return Unit::m;}; static constexpr int kg() {return Unit::kg;}; static constexpr int s() {return Unit::s;}; }; typedef Value<Unit<1, 0, -1> > Speed; // = / typedef Value<Unit<1, 0, -2> > Acceleration; // = // typedef Unit<1, 0, 0> M; typedef Unit<0, 0, 1> S;
Acceleration acc1 = Value<Unit<1, 0, -2> >(2); // = 2 //. . Acceleration acc2 = Value<M >(2); // . Speed sp1 = Value<Unit<1, 0, -2> >(2); // . Speed sp2 = Value<Unit<1, 0, -1> >(2); // = 2 /. .
template<class Value1, class Value2> auto operator/(Value1 v1, Value2 v2) -> Value<Unit<Value1::m() - Value2::m(), Value1::kg() - Value2::kg(), Value1::s() - Value2::s()> > { return Value<Unit<Value1::m() - Value2::m(), Value1::kg() - Value2::kg(), Value1::s() - Value2::s()> >(v1.val / v2.val);
Acceleration acc = Value<M>(100) / Value<S>(10) / Value<S>(1); // = 10 //. . Speed sp = Value<M>(100) / Value<S>(20); // = 5 /. .
Speed convertSpeed(KmPerHour val);
where the KmPerHour class is the elementary class needed to overload the convertSpeed function. Use as many unique classes as possible, it will help to use function overloading and save you from having to use different names for ideologically identical operations (convertSpeed (KmPerHour) and convertSpeed (KmPerSec) against convertSpeedFromKmPerHour (double) and convertSpeedFromKmPerSec (double)). Speed sp =100m/20s; // operator"" s(double) operator"" d(double).
Source: https://habr.com/ru/post/144334/
All Articles