docs: "UDLs vs Unit Constants" chapter extended

This commit is contained in:
Mateusz Pusz
2021-02-17 17:54:12 +01:00
parent 8193065feb
commit 86584dbfe2

View File

@@ -118,6 +118,7 @@ UDLs vs Unit Constants
UDLs are helpful but they also have some disadvantages compared to Unit Constants:
1. UDLs are only for compile-time known values and do not work for runtime variables
- UDLs::
using namespace units::physical::si::literals;
@@ -130,10 +131,12 @@ UDLs are helpful but they also have some disadvantages compared to Unit Constant
auto v1 = 120 * km / (2 * h);
auto v2 = distance * km / (duration * h);
Constants treat both cases in a unified way. It is also worth to notice that we work mostly with runtime variables
and compile-time known values mostly appear only in physical constants and unit tests.
Constants treat both cases in a unified way. It is also worth to notice that we work
mostly with runtime variables and compile-time known values mostly appear only in physical
constants and unit tests.
2. UDLs cannot be disambiguated with a namespace name
- UDLs::
using namespace units::physical::si::literals;
@@ -148,7 +151,9 @@ UDLs are helpful but they also have some disadvantages compared to Unit Constant
auto d1 = 1. * si_cm; // si::length<si::centimetre>
auto d2 = 1. * cgs_cm; // si::cgs::length<si::centimetre>
3. Poor control over the representation types as UDLs return only ``std::int64_t`` or ``long double``
3. Poor control over the representation types as UDLs return only ``std::int64_t`` or
``long double``
- UDLs::
using namespace units::physical::si::literals;
@@ -168,14 +173,24 @@ UDLs are helpful but they also have some disadvantages compared to Unit Constant
auto d6 = 123ll * km; // si::length<si::kilometre, long long>
4. UDLs are verbose to define and standardize
- UDLs:
- for each unit an integral and a floating-point UDL have to be defined
- have to be provided for unnamed derived units (i.e. ``_q_km_per_h``)
- Unit Constants:
- one constant per unit
- unnamed derived units constructed from base constants (i.e. ``km / h``)
5. Typical UDL definition for quantities when compiled with a ``-Wsign-conversion``
flag results in a compilation warning. This warning could be silenced with a
``static_cast<std::int64_t>(value)`` in every UDL, but in a such case other safety
and security issues could be silently introduced.
Unit Constants, on the opposite, always use the exact representation type provided
by the user so there is no chance for a truncating conversion on a quantity construction.
The only issue we are aware of with Unit Constants is a potential problem of specifying
a quantity in denominator::