mirror of
https://github.com/mpusz/mp-units.git
synced 2025-08-06 13:44:27 +02:00
docs: "UDLs vs Unit Constants" chapter extended
This commit is contained in:
@@ -118,6 +118,7 @@ UDLs vs Unit Constants
|
|||||||
UDLs are helpful but they also have some disadvantages compared to 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
|
1. UDLs are only for compile-time known values and do not work for runtime variables
|
||||||
|
|
||||||
- UDLs::
|
- UDLs::
|
||||||
|
|
||||||
using namespace units::physical::si::literals;
|
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 v1 = 120 * km / (2 * h);
|
||||||
auto v2 = distance * km / (duration * 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
|
Constants treat both cases in a unified way. It is also worth to notice that we work
|
||||||
and compile-time known values mostly appear only in physical constants and unit tests.
|
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
|
2. UDLs cannot be disambiguated with a namespace name
|
||||||
|
|
||||||
- UDLs::
|
- UDLs::
|
||||||
|
|
||||||
using namespace units::physical::si::literals;
|
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 d1 = 1. * si_cm; // si::length<si::centimetre>
|
||||||
auto d2 = 1. * cgs_cm; // si::cgs::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::
|
- UDLs::
|
||||||
|
|
||||||
using namespace units::physical::si::literals;
|
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>
|
auto d6 = 123ll * km; // si::length<si::kilometre, long long>
|
||||||
|
|
||||||
4. UDLs are verbose to define and standardize
|
4. UDLs are verbose to define and standardize
|
||||||
|
|
||||||
- UDLs:
|
- UDLs:
|
||||||
|
|
||||||
- for each unit an integral and a floating-point UDL have to be defined
|
- 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``)
|
- have to be provided for unnamed derived units (i.e. ``_q_km_per_h``)
|
||||||
|
|
||||||
- Unit Constants:
|
- Unit Constants:
|
||||||
|
|
||||||
- one constant per unit
|
- one constant per unit
|
||||||
- unnamed derived units constructed from base constants (i.e. ``km / h``)
|
- 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
|
The only issue we are aware of with Unit Constants is a potential problem of specifying
|
||||||
a quantity in denominator::
|
a quantity in denominator::
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user