Conversion(3)

Conversion(3) Unit Conversion and Dimensional Analysis Library Conversion(3)

NAME

Conversion - Explicit Conversion


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

SYNOPSIS

Functions


template<class UnitFrom, class UnitTo, typename T = double> static constexpr T units::convert (const T &value) noexcept
converts a value from one type to another. 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.

Detailed Description

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

Function Documentation

template<class UnitFrom, class UnitTo, typename T = double> constexpr T units::convert (const T & value) [inline], [static], [constexpr], [noexcept]

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

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

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.

template<typename T, typename Units, class = std::enable_if_t<std::is_arithmetic<T>::value && traits::is_unit_t<Units>::value>> T units::unit_cast (const Units & value) [inline], [constexpr], [noexcept]

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

T Type to cast the unit type to. Must be a built-in arithmetic type.

Parameters

value Unit value to cast.

See also

unit_t::to

Author

Generated automatically by Doxygen for Unit Conversion and Dimensional Analysis Library from the source code.

Wed Feb 11 2026 09:38:34 Version 2.3.0