Unit Conversion and Dimensional Analysis Library 3.6.1
A compile-time, header-only C++23 dimensional-analysis library
Loading...
Searching...
No Matches
Explicit Conversion

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

Functions

template<ConversionFactorType ConversionFactorFrom, ConversionFactorType ConversionFactorTo, ArithmeticType To = double, ArithmeticType From>
requires (traits::is_same_dimension_conversion_factor_v<ConversionFactorFrom, ConversionFactorTo>)
constexpr To units::convert (const From &value) noexcept
 converts a value from an unit to another.
template<UnitType UnitTo, UnitType UnitFrom>
requires same_dimension<UnitFrom, UnitTo>
constexpr UnitTo units::convert (const UnitFrom &from) noexcept
 converts an unit to another unit.
template<ArithmeticType T, UnitType Unit>
constexpr T units::unit_cast (const Unit &value) noexcept
 Casts an unit to an arithmetic type.

Detailed Description

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

Function Documentation

◆ convert() [1/2]

template<ConversionFactorType ConversionFactorFrom, ConversionFactorType ConversionFactorTo, ArithmeticType To = double, ArithmeticType From>
requires (traits::is_same_dimension_conversion_factor_v<ConversionFactorFrom, ConversionFactorTo>)
To units::convert ( const From & value)
constexprnoexcept

converts a value from an unit to another.

Converts a value of an arithmetic type to another unit. E.g.

double result =
convert<meters, feet>(1.0); // result == 3.28084
constexpr To convert(const From &value) noexcept
converts a value from an unit to another.
Definition core.h:2345

Intermediate computations are carried in the widest representation before being converted to To. is_same_dimension_conversion_factor_v<ConversionFactorFrom, ConversionFactorTo> shall be true.

See also
unit for implicit conversion of units.
Template Parameters
ConversionFactorFromconversion factor of the unit to convert value from. is_conversion_factor_v<ConversionFactorFrom> shall be true.
ConversionFactorToconversion factor of the unit to convert value to. is_conversion_factor_v<ConversionFactorTo> shall be true.
Fromtype of value. Shall be an arithmetic type.
Parameters
[in]valueArithmetic value to convert. The value should represent a quantity in units of ConversionFactorFrom.
Template Parameters
Totype of the converted unit value. Shall be an arithmetic type.
Returns
value, converted from units of ConversionFactorFrom to ConversionFactorTo. The value represents a quantity in units of ConversionFactorTo.

◆ convert() [2/2]

template<UnitType UnitTo, UnitType UnitFrom>
requires same_dimension<UnitFrom, UnitTo>
UnitTo units::convert ( const UnitFrom & from)
constexprnoexcept

converts an unit to another unit.

Converts the value of an unit to another unit. E.g.

meter_t result =
convert<meters>(foot_t(1.0)); // result == 3.28084_m

Intermediate computations are carried in the widest representation before being converted to UnitTo. is_same_dimension_unit_v<UnitFrom, UnitTo> shall be true.

See also
unit for implicit conversion of unit containers.
Template Parameters
UnitFromunit to convert to UnitTo. is_unit_v<UnitFrom> shall be true.
UnitTounit to convert from to. is_unit_v<UnitTo> shall be true.
Returns
from, converted from units of UnitFrom to UnitTo.

◆ unit_cast()

template<ArithmeticType T, UnitType Unit>
T units::unit_cast ( const Unit & value)
constexprnoexcept

Casts an unit to an arithmetic type.

unit_cast can be used to remove the strong typing from an unit class, and convert it to an arithmetic type. This may be useful for compatibility with libraries and legacy code that don't support unit types. E.g

meter_t unitVal(5);
double value = units::unit_cast<double>(unitVal); // value == 5.0
constexpr T unit_cast(const Unit &value) noexcept
Casts an unit to an arithmetic type.
Definition core.h:3754
Template Parameters
TType to cast the unit type to. Shall be an arithmetic type.
UnitType of the unit to cast to.
Parameters
valueUnit value to cast.
See also
unit::to