WPILibC++ 2024.3.2
Explicit Conversion

Functions used to convert values of one logical type to another. More...

Functions

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...
 

Detailed Description

Functions used to convert values of one logical type to another.

Function Documentation

◆ 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); // result == 3.28084
See also
unit_t for implicit conversion of unit containers.
Template Parameters
UnitFromunit 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).
UnitTounit 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).
Ttype of value. It is inferred from value, and is expected to be a built-in arithmetic type.
Parameters
[in]valueArithmetic 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); // value = 5.0
Template Parameters
TType to cast the unit type to. Must be a built-in arithmetic type.
Parameters
valueUnit value to cast.
See also
unit_t::to