3 ms·
C++ has, in recent years, improved support for this. The best state of affairs is with time units: using namespace std::chrono_literals; auto lesson =
by einpoklum 5y ago
C++ has, in recent years, improved support for this. The best state of affairs is with time units:
using namespace std::chrono_literals;
auto lesson = 45min;
auto day = 24h;
std::cout << "one lesson is " << lesson.count() << " minutes\n"
<< "one day is " << day.count() << " hours\n";
and you can compare durations specified with different units etc. There mpusz/units library, which may go into the standard at some point, lets you do this:
using namespace units::isq::si::references;
// simple numeric operations
static_assert(10 * km / 2 == 5 * km);
// unit conversions
static_assert(1 * h == 3600 * s);
static_assert(1 * km + 1 * m == 1001 * m);
and also write conceptified functions which "do the right thing", e.g.:
constexpr Speed auto avg_speed(Length auto d, Time auto t)
{
return d / t;
}
this will normalize appropriately. But - the specific units used will need to be known at compile-time. Choosing units at run-time is a different kettle of fish.