Functions used to convert values of one logical type to another.
More...
|
template<class UnitFrom , class UnitTo , typename T = UNIT_LIB_DEFAULT_TYPE> |
static constexpr T | units::convert (const T &value) noexcept |
| converts a value from one type to another. More...
|
|
template<typename T , typename Units , class = std::enable_if_t<std::is_arithmetic<T>::value && traits::is_unit_t<Units>::value>> |
constexpr T | units::unit_cast (const Units &value) noexcept |
| Casts a unit container to an arithmetic type. More...
|
|
Functions used to convert values of one logical type to another.
◆ convert()
template<class UnitFrom , class UnitTo , typename T = UNIT_LIB_DEFAULT_TYPE>
static constexpr T units::convert |
( |
const T & |
value | ) |
|
|
inlinestaticconstexprnoexcept |
converts a value from one type to another.
Converts a value of a built-in arithmetic type to another unit. This does not change the type of value, only what it contains. E.g.
double result = convert<length::meters, length::feet>(1.0);
- See also
- unit_t for implicit conversion of unit containers.
- Template Parameters
-
UnitFrom | unit tag to convert value from. Must be a unit type (i.e. is_unit<UnitFrom>::value == true), and must be convertible to UnitTo (i.e. is_convertible_unit<UnitFrom, UnitTo>::value == true). |
UnitTo | unit tag to convert value to. Must be a unit type (i.e. is_unit<UnitTo>::value == true), and must be convertible from UnitFrom (i.e. is_convertible_unit<UnitFrom, UnitTo>::value == true). |
T | type of value. It is inferred from value, and is expected to be a built-in arithmetic type. |
- Parameters
-
[in] | value | Arithmetic value to convert from UnitFrom to UnitTo . The value should represent a quantity in units of UnitFrom . |
- Returns
- value, converted from units of
UnitFrom
to UnitTo
.
◆ unit_cast()
template<typename T , typename Units , class = std::enable_if_t<std::is_arithmetic<T>::value && traits::is_unit_t<Units>::value>>
constexpr T units::unit_cast |
( |
const Units & |
value | ) |
|
|
inlineconstexprnoexcept |
Casts a unit container to an arithmetic type.
unit_cast can be used to remove the strong typing from a unit class, and convert it to a built-in arithmetic type. This may be useful for compatibility with libraries and legacy code that don't support unit_t
types. E.g
meter_t unitVal(5);
double value = units::unit_cast<double>(unitVal);
- Template Parameters
-
T | Type to cast the unit type to. Must be a built-in arithmetic type. |
- Parameters
-
- See also
- unit_t::to