Quantity
Dimensioned quantity with compile-time unit checking/conversion
Context
#include <xo/unit/quantity.hpp>
Arithmetic on xo::qty::quantity instances does not use
xo::qty::quantity::s_scaled_unitat runtime; instead gets everything it needs at compile time.The
xo::qty::quantitytemplate takes a xo::qty::scaled_unit instance, but only accepts values withxo::qty::scaled_unit::is_natural() == true.This accomodation (instead of requiring a xo::qty::natural_unit instance is to make possible code like this possible:
#include "xo/unit/quantity.hpp" using namespace xo::qty; quantity<u::meter / u::second> x; quantity<u::meter * u::mter> y;
while rejecting attempt to mix multiple scales in the same quantity value:
quantity<u::meter * u::millimeter> x; // will not compile
Class
The primary data structure for interacting with xo-unit is the
template class xo::qty::quantity.
A quantity is a compile-time wrapper around a single arithmetic value,
with type taken from the Repr parameter in quantity<Unit, Repr>.
-
template<auto ScaledUnit, typename Repr = double>
class quantity represent a scalar quantity with associated units.
Enforce dimensional consistency at compile time. sizeof(quantity) == sizeof(Repr).
Unit information is associated with type, not value. A quantity’s runtime state consists of exactly one
Reprinstance:sizeof(quantity<NaturalUnit, Repr>) == sizeof(Repr)
- Template Parameters:
ScaledUnit – is a non-type template paramoeter identifying a unit used for this quantity. In xo-unit it will be an instance of
natural_unitRepr – is a type used to represent a multiple of
ScaledUnit.
Member Variables
- group Quantity-static-vars
Variables
-
static scaled_unit<ratio_int_type> s_scaled_unit = ScaledUnit
unit for quantity of this type. Determined at compile-time
-
static scaled_unit<ratio_int_type> s_scaled_unit = ScaledUnit
- group Quantity-instance-vars
Variables
-
Repr scale_ = Repr{}
quantity represents this multiple of s_scaled_unit
Public to avoid disqualifying
quantityas a ‘structural type’; prerequisite for using aquantityinstance as a non-type template parameter
-
Repr scale_ = Repr{}
Type Traits
- group quantity type traits
Constructors
- group quantity constructors
The simplest way to create a quantity instance is to use either
factory functions in
xo::qty::qty, see Quantity Factory Functionsunit variables in
xo::qty::qty, see Quantity Unit Variables
Assignment
- group quantity assignment operators
Access Methods
- group quantity access methods
Functions
-
inline const repr_type &scale() const
value of
scale_in quantity representing amount (scale_*s_unit)
-
inline bool is_negative() const
true iff this quantity is strictly negative
-
inline bool is_positive() const
true iff this quantity is strictly positive
-
static inline bool is_dimensionless()
true iff this quantity represents a dimensionless value
-
inline nu_abbrev_type abbrev() const
abbreviated suffix for quantities with this unit
-
inline const repr_type &scale() const
Constants
- group static quantity constants
Conversion Methods
Amount-preserving conversion to quantities with different units and/or representation.
- group Quantity-unit-conversion
Functions
-
template<typename Repr2>
inline auto with_repr() const create equivalent quantity using scale representation
Repr2instead ofRepr
-
template<natural_unit<ratio_int_type> NaturalUnit2>
inline auto rescale() const create equivalent quantity expressed as a multiple of
NaturalUnit2instead of s_unit
-
template<scaled_unit<ratio_int_type> ScaledUnit2>
inline auto rescale_ext() const create equivalent quantity expressed as as multiple of
ScaledUnit2instead of s_unit
-
inline constexpr operator Repr() const
For dimensionless quantities: convert to underlying scale value
Not present for dimensioned quantities.
-
template<typename Repr2>
Arithmetic
- group Quantity-operators
Functions
-
template<typename Quantity2>
inline quantity &operator+=(const Quantity2 &y) add
yin-place, converting units if necessary
-
template<typename Quantity2>
inline quantity &operator-=(const Quantity2 &y) subtract
yin-place, converting units if necessary
-
template<typename Dimensionless>
inline quantity &operator*=(Dimensionless y) multiply
yin-place. y must be dimensionless
-
template<typename Dimensionless>
inline quantity &operator/=(Dimensionless y) divide
yin-place. y must be dimensionless
-
template<typename Q1, typename Q2>
auto operator*(const Q1 &x, const Q2 &y) note: won’t have constexpr result w/ fractional dimension until c++26 (when
sqrt(),pow()are constexpr)note: won’t have constexpr result until c++26 (when
sqrt(),pow()are constexpr)
-
template<typename Dimensionless, typename Quantity>
auto operator*(const Quantity &x, Dimensionless y) note: does not require unit scaling, so constexpr with c++23
-
template<typename Dimensionless, typename Quantity>
auto operator*(Dimensionless x, const Quantity &y) note: does not require unit scaling, so constexpr with c++23
-
template<typename Q1, typename Q2>
auto operator/(const Q1 &x, const Q2 &y) divide quantity
xby quantityy.note: won’t have constexpr result w/ fractional dimension until c++26 (when
sqrt(),pow()are constexpr)
-
template<typename Dimensionless, typename Quantity>
auto operator/(const Quantity &x, Dimensionless y) divide quantity
xby dimensionless valueynote: doesn not require unit scaling, so constexpr with c++23
-
template<typename Dimensionless, typename Quantity>
auto operator/(Dimensionless x, const Quantity &y) divide dimensionless value
xby quantityynote: doesn not require unit scaling, so constexpr with c++23
-
template<typename Q1, typename Q2>
auto operator+(const Q1 &x, const Q2 &y) add quantity
yto quantityx. Result will have the same units asx. Representation will be the widest of {x::repr_type,y::repr_type}.note: won’t have constexpr result w/ fractional dimension until c++26 (when
sqrt(),pow()are constexpr)- Pre:
xandyexpected to have consistent dimensions
-
template<typename Quantity, typename Dimensionless>
auto operator+(const Quantity &x, Dimensionless y) subtract an arithmetic value from a dimensionless quantity
-
template<typename Dimensionless, typename Quantity>
auto operator+(Dimensionless x, const Quantity &y) subtract a dimensionless quantity from an arithmetic value
-
template<typename Q1, typename Q2>
auto operator-(const Q1 &x, const Q2 &y) subtract quantity
yfrom quantityx. Result will have the same units asx. Representation will be the widest of {x::repr_type,y::repr_type}note: won’t have constexpr result w/ fractional dimension until c++26 (when
sqrt(),pow()are constexpr)- Pre:
xandyexpected to have consistent dimensions
-
template<typename Quantity, typename Dimensionless>
auto operator-(const Quantity &x, Dimensionless y) subtract an arithmetic value from a dimensionless quantity
-
template<typename Dimensionless, typename Quantity>
auto operator-(Dimensionless x, const Quantity &y) subtract a dimensionless quantity from an arithmetic value
-
template<typename Quantity2>
Support methods for arithmetic operations
- group Quantity-arithmetic-support
Functions
-
inline auto unit_qty() const
create unit quantity with same unit as
this
-
inline auto zero_qty() const
create zero quantity with same unit as
this
-
inline auto reciprocal() const
-
template<typename Dimensionless>
inline auto scale_by(Dimensionless x) const create quantity representing this amount multiplied by dimensionless value
x- Pre:
x must be an arithmetic type such as
intordouble
-
template<typename Dimensionless>
inline auto divide_by(Dimensionless x) const create quantity representing this quantity divided by dimensionless value
x- Pre:
x must be an arithmetic type such as
intordouble
-
template<typename Dimensionless>
inline auto divide_into(Dimensionless x) const create quantity representing dimensionless value
xdivided by this quantity- Pre:
x must be an arithmetic type such as
intordouble
-
inline auto unit_qty() const
Comparison
Support methods for comparison operators
- group Quantity-comparison-support