Unit Conversion and Dimensional Analysis Library 3.6.1
A compile-time, header-only C++23 dimensional-analysis library
Loading...
Searching...
No Matches
units::unit< ConversionFactor, T, NumericalScale > Class Template Reference
Inheritance diagram for units::unit< ConversionFactor, T, NumericalScale >:
[legend]
Collaboration diagram for units::unit< ConversionFactor, T, NumericalScale >:
[legend]

Public Types

using numerical_scale_type = NumericalScale
using underlying_type = T
 < Type of the numerical scale of the unit (e.g. linear_scale)
using value_type = T
 < Type of the underlying storage of the unit (e.g. double)
using conversion_factor = ConversionFactor
 < Synonym for underlying type. May be removed in future versions. Prefer underlying_type.

Public Member Functions

constexpr unit ()=default
 < Type of conversion_factor the unit represents (e.g. meters)
constexpr unit (const unit &)=default
 default copy constructor.
template<ConversionFactorType ConversionFactorRhs, ArithmeticType Ty, NumericalScaleType< Ty > NsRhs>
requires traits::is_same_dimension_unit_v<unit<ConversionFactorRhs, Ty, NsRhs>, unit> && detail::is_losslessly_convertible_unit
<unit<ConversionFactorRhs, Ty, NsRhs>, unit>
constexpr unit (const unit< ConversionFactorRhs, Ty, NsRhs > &rhs) noexcept
 converting constructor
template<ConversionFactorType ConversionFactorRhs, ArithmeticType Ty, NumericalScaleType< Ty > NsRhs>
requires (traits::is_same_dimension_unit_v<unit<ConversionFactorRhs, Ty, NsRhs>, unit> && !detail::is_losslessly_convertible_unit
<unit<ConversionFactorRhs, Ty, NsRhs>, unit> && std::is_floating_point_v<Ty> && std::is_integral_v<T>)
consteval unit (const unit< ConversionFactorRhs, Ty, NsRhs > &rhs)
 compile-time narrowing converting constructor
template<ConversionFactorType ConversionFactorRhs, ArithmeticType Ty, NumericalScaleType< Ty > NsRhs>
requires (traits::is_same_dimension_unit_v<unit<ConversionFactorRhs, Ty, NsRhs>, unit> && !detail::is_losslessly_convertible_unit
<unit<ConversionFactorRhs, Ty, NsRhs>, unit> && std::is_integral_v<Ty> && std::is_integral_v<T>)
consteval unit (const unit< ConversionFactorRhs, Ty, NsRhs > &rhs)
 compile-time exact integral converting constructor
template<ArithmeticType Ty>
requires (!traits::is_dimensionless_unit<ConversionFactor>::value && detail::is_losslessly_convertible<Ty, T>)
constexpr unit (Ty value) noexcept
 constructor
template<ArithmeticType Ty>
requires detail::is_losslessly_convertible<Ty, T>
constexpr unit (Ty value, linearized_value_t) noexcept
 constructor
template<ArithmeticType Ty>
requires traits::is_dimensionless_unit<ConversionFactor>::value && detail::is_losslessly_convertible<Ty, T>
constexpr unit (Ty value) noexcept
 constructor
template<ArithmeticType Rep, RatioType Period>
requires detail::is_time_conversion_factor<ConversionFactor> && detail::is_losslessly_convertible<Rep, T> &&
detail::is_losslessly_convertible_unit<units::unit<units::conversion_factor<Period, dimension::time>, Rep>, unit>
constexpr unit (const std::chrono::duration< Rep, Period > &value) noexcept
 chrono constructor
constexpr unit & operator= (const unit &rhs) noexcept=default
 default assignment
template<ConversionFactorType Cf = ConversionFactor>
requires traits::is_dimensionless_unit<Cf>::value
constexpr unit & operator= (const underlying_type &rhs) noexcept
 assignment
template<ConversionFactorType ConversionFactorRhs, ArithmeticType Ty, NumericalScaleType< Ty > NsRhs>
constexpr bool operator< (const unit< ConversionFactorRhs, Ty, NsRhs > &rhs) const noexcept
 less-than
template<ConversionFactorType ConversionFactorRhs, ArithmeticType Ty, NumericalScaleType< Ty > NsRhs>
constexpr bool operator<= (const unit< ConversionFactorRhs, Ty, NsRhs > &rhs) const noexcept
 less-than or equal
template<ConversionFactorType ConversionFactorRhs, ArithmeticType Ty, NumericalScaleType< Ty > NsRhs>
constexpr bool operator> (const unit< ConversionFactorRhs, Ty, NsRhs > &rhs) const noexcept
 greater-than
template<ConversionFactorType ConversionFactorRhs, ArithmeticType Ty, NumericalScaleType< Ty > NsRhs>
constexpr bool operator>= (const unit< ConversionFactorRhs, Ty, NsRhs > &rhs) const noexcept
 greater-than or equal
template<ConversionFactorType ConversionFactorRhs, ArithmeticType Ty, NumericalScaleType< Ty > NsRhs>
requires (std::floating_point<T> || std::floating_point<Ty>)
constexpr bool operator== (const unit< ConversionFactorRhs, Ty, NsRhs > &rhs) const noexcept
 equality
template<ConversionFactorType ConversionFactorRhs, ArithmeticType Ty, NumericalScaleType< Ty > NsRhs>
requires (std::integral<T> && std::integral<Ty>)
constexpr bool operator== (const unit< ConversionFactorRhs, Ty, NsRhs > &rhs) const noexcept
template<ConversionFactorType ConversionFactorRhs, ArithmeticType Ty, NumericalScaleType< Ty > NsRhs>
constexpr bool operator!= (const unit< ConversionFactorRhs, Ty, NsRhs > &rhs) const noexcept
 inequality
constexpr underlying_type raw () const noexcept
 scaled unit value
constexpr auto value () const noexcept
 unit value
template<ArithmeticType Ty>
constexpr Ty to () const noexcept
 unit value
template<template< class > class UnitType>
requires same_dimension<UnitType<T>, unit>
constexpr UnitType< T > to () const noexcept
 conversion to a named unit
constexpr T to_linearized () const noexcept
 linearized unit value
template<ConversionFactorType Cf, ArithmeticType Ty = T>
constexpr unit< Cf, Ty > convert () const noexcept
 conversion
template<template< class > class UnitType>
requires same_dimension<UnitType<T>, unit>
constexpr UnitType< T > convert () const noexcept
 conversion
template<ArithmeticType Ty>
requires traits::is_dimensionless_unit<ConversionFactor>::value
constexpr operator Ty () const noexcept
 implicit type unsafe conversion.
template<ArithmeticType Ty>
requires (!traits::is_dimensionless_unit<ConversionFactor>::value)
constexpr operator Ty () const noexcept
 explicit type unsafe conversion.
template<ArithmeticType Rep, RatioType Period, ConversionFactorType Cf = ConversionFactor>
requires detail::is_time_conversion_factor<Cf> && detail::is_losslessly_convertible<T, Rep>
constexpr operator std::chrono::duration< Rep, Period > () const noexcept
 chrono implicit type conversion.
template<UnitType Unit = unit>
constexpr const char * name () const noexcept
 returns the unit name
template<UnitType Unit = unit>
constexpr const char * abbreviation () const noexcept
 returns the unit abbreviation

Public Attributes

_linearized_value
 Not to be used as part of the official API, this member is public to allow the use of units as NTTP types.

Additional Inherited Members

Static Public Member Functions inherited from units::linear_scale
template<class T>
static constexpr T linearize (const T value) noexcept
 linearizes value
template<class T>
static constexpr T scale (const T value) noexcept
 scales value

Constructor & Destructor Documentation

◆ unit() [1/7]

template<ConversionFactorType ConversionFactor, ArithmeticType T = double, NumericalScaleType< T > NumericalScale = linear_scale>
template<ConversionFactorType ConversionFactorRhs, ArithmeticType Ty, NumericalScaleType< Ty > NsRhs>
requires traits::is_same_dimension_unit_v<unit<ConversionFactorRhs, Ty, NsRhs>, unit> && detail::is_losslessly_convertible_unit
<unit<ConversionFactorRhs, Ty, NsRhs>, unit>
units::unit< ConversionFactor, T, NumericalScale >::unit ( const unit< ConversionFactorRhs, Ty, NsRhs > & rhs)
inlineconstexprnoexcept

converting constructor

performs implicit unit conversions if required.

Parameters
[in]rhsunit to copy.

◆ unit() [2/7]

template<ConversionFactorType ConversionFactor, ArithmeticType T = double, NumericalScaleType< T > NumericalScale = linear_scale>
template<ConversionFactorType ConversionFactorRhs, ArithmeticType Ty, NumericalScaleType< Ty > NsRhs>
requires (traits::is_same_dimension_unit_v<unit<ConversionFactorRhs, Ty, NsRhs>, unit> && !detail::is_losslessly_convertible_unit
<unit<ConversionFactorRhs, Ty, NsRhs>, unit> && std::is_floating_point_v<Ty> && std::is_integral_v<T>)
units::unit< ConversionFactor, T, NumericalScale >::unit ( const unit< ConversionFactorRhs, Ty, NsRhs > & rhs)
inlineconsteval

compile-time narrowing converting constructor

Constructs an integral-underlying unit from a same-dimension floating-point one when the conversion is exact — the case the ordinary converting constructor rejects as lossy. It is consteval, so it participates only in a constant-evaluated context (feet<int> f = 16_ft;); a value not exactly representable in the target (a fractional or out-of-range magnitude, e.g. 16.5_ft) makes the constructor a non-constant expression and the program ill-formed. A run-time floating-to-integral unit conversion remains rejected. Wholeness is judged on the stored point count (raw()), so a ratio-dimensionless unit converts correctly too (percent<int> p = 1_pct; is percent<int> holding 1, not a rejected 0.01).

Parameters
[in]rhsunit to convert.

◆ unit() [3/7]

template<ConversionFactorType ConversionFactor, ArithmeticType T = double, NumericalScaleType< T > NumericalScale = linear_scale>
template<ConversionFactorType ConversionFactorRhs, ArithmeticType Ty, NumericalScaleType< Ty > NsRhs>
requires (traits::is_same_dimension_unit_v<unit<ConversionFactorRhs, Ty, NsRhs>, unit> && !detail::is_losslessly_convertible_unit
<unit<ConversionFactorRhs, Ty, NsRhs>, unit> && std::is_integral_v<Ty> && std::is_integral_v<T>)
units::unit< ConversionFactor, T, NumericalScale >::unit ( const unit< ConversionFactorRhs, Ty, NsRhs > & rhs)
inlineconsteval

compile-time exact integral converting constructor

Constructs an integral-underlying unit from a same-dimension integral one of a FINER unit when the value is an exact whole number of this (coarser) unit — the case the ordinary converting constructor rejects as potentially-lossy because a run-time value need not divide evenly (bytes<int> b = 16_bit; is 2, but a run-time bits<int> might be 17). It is consteval, so it participates only in a constant-evaluated context; a value that is not a whole number of the target unit (17_bit into bytes<int>) makes the constructor a non-constant expression and the program ill-formed — never a silent truncation. The exactness test is exact integer arithmetic in a double-width intermediate, so it cannot be defeated by an intermediate overflow. A run-time integral-to-coarser-integral unit conversion remains rejected; use round/floor/ ceil/trunc<To> for a deliberate run-time rounding.

Parameters
[in]rhsunit to convert.

◆ unit() [4/7]

template<ConversionFactorType ConversionFactor, ArithmeticType T = double, NumericalScaleType< T > NumericalScale = linear_scale>
template<ArithmeticType Ty>
requires (!traits::is_dimensionless_unit<ConversionFactor>::value && detail::is_losslessly_convertible<Ty, T>)
units::unit< ConversionFactor, T, NumericalScale >::unit ( Ty value)
inlineexplicitconstexprnoexcept

constructor

constructs a new unit with value.

Parameters
[in]valueunit magnitude.

◆ unit() [5/7]

template<ConversionFactorType ConversionFactor, ArithmeticType T = double, NumericalScaleType< T > NumericalScale = linear_scale>
template<ArithmeticType Ty>
requires detail::is_losslessly_convertible<Ty, T>
units::unit< ConversionFactor, T, NumericalScale >::unit ( Ty value,
linearized_value_t  )
inlineexplicitconstexprnoexcept

constructor

constructs a new unit with value.

Parameters
[in]valuelinearized unit magnitude.

◆ unit() [6/7]

template<ConversionFactorType ConversionFactor, ArithmeticType T = double, NumericalScaleType< T > NumericalScale = linear_scale>
template<ArithmeticType Ty>
requires traits::is_dimensionless_unit<ConversionFactor>::value && detail::is_losslessly_convertible<Ty, T>
units::unit< ConversionFactor, T, NumericalScale >::unit ( Ty value)
inlineconstexprnoexcept

constructor

enable implicit conversions from T types ONLY for linear dimensionless units

Parameters
[in]valuevalue of the unit

◆ unit() [7/7]

template<ConversionFactorType ConversionFactor, ArithmeticType T = double, NumericalScaleType< T > NumericalScale = linear_scale>
template<ArithmeticType Rep, RatioType Period>
requires detail::is_time_conversion_factor<ConversionFactor> && detail::is_losslessly_convertible<Rep, T> &&
detail::is_losslessly_convertible_unit<units::unit<units::conversion_factor<Period, dimension::time>, Rep>, unit>
units::unit< ConversionFactor, T, NumericalScale >::unit ( const std::chrono::duration< Rep, Period > & value)
inlineconstexprnoexcept

chrono constructor

enable implicit conversions from std::chrono::duration types ONLY for time units

Parameters
[in]valuevalue of the unit

Member Function Documentation

◆ convert() [1/2]

template<ConversionFactorType ConversionFactor, ArithmeticType T = double, NumericalScaleType< T > NumericalScale = linear_scale>
template<template< class > class UnitType>
requires same_dimension<UnitType<T>, unit>
UnitType< T > units::unit< ConversionFactor, T, NumericalScale >::convert ( ) const
inlineconstexprnoexcept

conversion

Converts to a different unit. Units can be converted to other units implicitly, but this can be used in cases where the explicit notation of a conversion is beneficial, or where a prvalue unit is needed.

Template Parameters
UnitTypeunit type to convert to
Returns
a unit with the specified parameters containing the equivalent value to *this.

◆ convert() [2/2]

template<ConversionFactorType ConversionFactor, ArithmeticType T = double, NumericalScaleType< T > NumericalScale = linear_scale>
template<ConversionFactorType Cf, ArithmeticType Ty = T>
unit< Cf, Ty > units::unit< ConversionFactor, T, NumericalScale >::convert ( ) const
inlineconstexprnoexcept

conversion

Converts to a different unit. Units can be converted to other units implicitly, but this can be used in cases where the explicit notation of a conversion is beneficial, or where an prvalue unit is needed.

Template Parameters
Cfconversion factor of the unit to convert to
Tyunderlying type of the unit to convert to
Returns
a unit with the specified parameters containing the equivalent value to *this.

◆ operator std::chrono::duration< Rep, Period >()

template<ConversionFactorType ConversionFactor, ArithmeticType T = double, NumericalScaleType< T > NumericalScale = linear_scale>
template<ArithmeticType Rep, RatioType Period, ConversionFactorType Cf = ConversionFactor>
requires detail::is_time_conversion_factor<Cf> && detail::is_losslessly_convertible<T, Rep>
units::unit< ConversionFactor, T, NumericalScale >::operator std::chrono::duration< Rep, Period > ( ) const
inlineconstexprnoexcept

chrono implicit type conversion.

only enabled for time unit types.

◆ operator Ty() [1/2]

template<ConversionFactorType ConversionFactor, ArithmeticType T = double, NumericalScaleType< T > NumericalScale = linear_scale>
template<ArithmeticType Ty>
requires (!traits::is_dimensionless_unit<ConversionFactor>::value)
units::unit< ConversionFactor, T, NumericalScale >::operator Ty ( ) const
inlineexplicitconstexprnoexcept

explicit type unsafe conversion.

only enabled for non-dimensionless unit types.

◆ operator Ty() [2/2]

template<ConversionFactorType ConversionFactor, ArithmeticType T = double, NumericalScaleType< T > NumericalScale = linear_scale>
template<ArithmeticType Ty>
requires traits::is_dimensionless_unit<ConversionFactor>::value
units::unit< ConversionFactor, T, NumericalScale >::operator Ty ( ) const
inlineconstexprnoexcept

implicit type unsafe conversion.

only enabled for dimensionless unit types.

◆ operator!=()

template<ConversionFactorType ConversionFactor, ArithmeticType T = double, NumericalScaleType< T > NumericalScale = linear_scale>
template<ConversionFactorType ConversionFactorRhs, ArithmeticType Ty, NumericalScaleType< Ty > NsRhs>
bool units::unit< ConversionFactor, T, NumericalScale >::operator!= ( const unit< ConversionFactorRhs, Ty, NsRhs > & rhs) const
inlineconstexprnoexcept

inequality

compares the linearized value of two units. Performs unit conversions if necessary.

Parameters
[in]rhsright-hand side unit for the comparison
Returns
true IFF the value of this is not equal to the value of rhs.
Note
This may not be suitable for all applications when the underlying_type of unit is a double.

◆ operator<()

template<ConversionFactorType ConversionFactor, ArithmeticType T = double, NumericalScaleType< T > NumericalScale = linear_scale>
template<ConversionFactorType ConversionFactorRhs, ArithmeticType Ty, NumericalScaleType< Ty > NsRhs>
bool units::unit< ConversionFactor, T, NumericalScale >::operator< ( const unit< ConversionFactorRhs, Ty, NsRhs > & rhs) const
inlineconstexprnoexcept

less-than

compares the linearized value of two units. Performs unit conversions if necessary.

Parameters
[in]rhsright-hand side unit for the comparison
Returns
true IFF the value of this is less than the value of rhs

◆ operator<=()

template<ConversionFactorType ConversionFactor, ArithmeticType T = double, NumericalScaleType< T > NumericalScale = linear_scale>
template<ConversionFactorType ConversionFactorRhs, ArithmeticType Ty, NumericalScaleType< Ty > NsRhs>
bool units::unit< ConversionFactor, T, NumericalScale >::operator<= ( const unit< ConversionFactorRhs, Ty, NsRhs > & rhs) const
inlineconstexprnoexcept

less-than or equal

compares the linearized value of two units. Performs unit conversions if necessary.

Parameters
[in]rhsright-hand side unit for the comparison
Returns
true IFF the value of this is less than or equal to the value of rhs

◆ operator=() [1/2]

template<ConversionFactorType ConversionFactor, ArithmeticType T = double, NumericalScaleType< T > NumericalScale = linear_scale>
template<ConversionFactorType Cf = ConversionFactor>
requires traits::is_dimensionless_unit<Cf>::value
unit & units::unit< ConversionFactor, T, NumericalScale >::operator= ( const underlying_type & rhs)
inlineconstexprnoexcept

assignment

performs implicit conversions from built-in types ONLY for dimensionless units

Parameters
[in]rhsvalue to copy.

◆ operator=() [2/2]

template<ConversionFactorType ConversionFactor, ArithmeticType T = double, NumericalScaleType< T > NumericalScale = linear_scale>
unit & units::unit< ConversionFactor, T, NumericalScale >::operator= ( const unit< ConversionFactor, T, NumericalScale > & rhs)
constexprdefaultnoexcept

default assignment

performs implicit unit conversions if required.

Parameters
[in]rhsunit to copy.

◆ operator==()

template<ConversionFactorType ConversionFactor, ArithmeticType T = double, NumericalScaleType< T > NumericalScale = linear_scale>
template<ConversionFactorType ConversionFactorRhs, ArithmeticType Ty, NumericalScaleType< Ty > NsRhs>
requires (std::floating_point<T> || std::floating_point<Ty>)
bool units::unit< ConversionFactor, T, NumericalScale >::operator== ( const unit< ConversionFactorRhs, Ty, NsRhs > & rhs) const
inlineconstexprnoexcept

equality

compares the linearized value of two units. Performs unit conversions if necessary.

Parameters
[in]rhsright-hand side unit for the comparison
Returns
true IFF the value of this exactly equal to the value of rhs.
Note
This may not be suitable for all applications when the underlying_type of unit is a double.

◆ operator>()

template<ConversionFactorType ConversionFactor, ArithmeticType T = double, NumericalScaleType< T > NumericalScale = linear_scale>
template<ConversionFactorType ConversionFactorRhs, ArithmeticType Ty, NumericalScaleType< Ty > NsRhs>
bool units::unit< ConversionFactor, T, NumericalScale >::operator> ( const unit< ConversionFactorRhs, Ty, NsRhs > & rhs) const
inlineconstexprnoexcept

greater-than

compares the linearized value of two units. Performs unit conversions if necessary.

Parameters
[in]rhsright-hand side unit for the comparison
Returns
true IFF the value of this is greater than the value of rhs

◆ operator>=()

template<ConversionFactorType ConversionFactor, ArithmeticType T = double, NumericalScaleType< T > NumericalScale = linear_scale>
template<ConversionFactorType ConversionFactorRhs, ArithmeticType Ty, NumericalScaleType< Ty > NsRhs>
bool units::unit< ConversionFactor, T, NumericalScale >::operator>= ( const unit< ConversionFactorRhs, Ty, NsRhs > & rhs) const
inlineconstexprnoexcept

greater-than or equal

compares the linearized value of two units. Performs unit conversions if necessary.

Parameters
[in]rhsright-hand side unit for the comparison
Returns
true IFF the value of this is greater than or equal to the value of rhs

◆ raw()

template<ConversionFactorType ConversionFactor, ArithmeticType T = double, NumericalScaleType< T > NumericalScale = linear_scale>
underlying_type units::unit< ConversionFactor, T, NumericalScale >::raw ( ) const
inlineconstexprnoexcept

scaled unit value

does NOT normalize dimensionless values.

Returns
value of the unit in it's underlying, non-safe type after applying the scale.

◆ to() [1/2]

template<ConversionFactorType ConversionFactor, ArithmeticType T = double, NumericalScaleType< T > NumericalScale = linear_scale>
template<template< class > class UnitType>
requires same_dimension<UnitType<T>, unit>
UnitType< T > units::unit< ConversionFactor, T, NumericalScale >::to ( ) const
inlineconstexprnoexcept

conversion to a named unit

Converts to a different named unit of the same dimension, e.g. (100.0_cm).to<meters>(). The named-template spelling of convert(); provided so a single accessor reads for both underlying-type extraction (to<double>()) and dimensioned conversion (to<meters>()).

Template Parameters
UnitTypeunit class template to convert to
Returns
a UnitType<T> containing the equivalent value to *this.

◆ to() [2/2]

template<ConversionFactorType ConversionFactor, ArithmeticType T = double, NumericalScaleType< T > NumericalScale = linear_scale>
template<ArithmeticType Ty>
Ty units::unit< ConversionFactor, T, NumericalScale >::to ( ) const
inlineconstexprnoexcept

unit value

Returns
value of the unit converted to an arithmetic, non-safe type.

◆ to_linearized()

template<ConversionFactorType ConversionFactor, ArithmeticType T = double, NumericalScaleType< T > NumericalScale = linear_scale>
T units::unit< ConversionFactor, T, NumericalScale >::to_linearized ( ) const
inlineconstexprnoexcept

linearized unit value

Returns
linearized value of unit which has a (possibly) non-linear scale.

◆ value()

template<ConversionFactorType ConversionFactor, ArithmeticType T = double, NumericalScaleType< T > NumericalScale = linear_scale>
auto units::unit< ConversionFactor, T, NumericalScale >::value ( ) const
inlineconstexprnoexcept

unit value

Normalizes dimensionless values to remove any scale factor they may have. E.g. percent(50).value() == 0.5.

Returns
value of the unit in it's underlying, non-safe type.

The documentation for this class was generated from the following file:
  • /home/runner/work/units/units/include/units/core.h