template<class Derived, class T>
Rodin::Math::Unit class

Base class for units using CRTP.

This class provides a generic base for creating type-safe units with value semantics and arithmetic operations. It uses the Curiously Recurring Template Pattern (CRTP) to enable derived classes to return their own type from operations.

Type Safety

Units provide compile-time type safety, preventing accidental mixing of incompatible units (e.g., adding radians to meters).

Arithmetic Operations

All standard arithmetic operations are supported:

  • Addition/subtraction: $ u_1 \pm u_2 $
  • Multiplication/division: $ u_1 \times u_2 $ , $ u_1 / u_2 $
  • Unary operations: $ +u $ , $ -u $
  • Compound assignments: +=, -=, *=, /=
  • Comparison: ==, !=, <, >, <=, >=

Example Usage

// Creating a derived unit class
class Meter : public Unit<Meter, Real>
{
  public:
    using Parent = Unit<Meter, Real>;
    using Parent::Parent;
};

// Using the unit
Meter length1(5.0);
Meter length2(3.0);
Meter sum = length1 + length2;  // 8.0 meters

Public static functions

static auto One() -> Derived
Creates a unit with value 1.
static auto Zero() -> Derived
Creates a unit with value 0.

Constructors, destructors, conversion operators

Unit(T v) constexpr
Constructs a unit from a value.
operator T() const explicit
Explicit conversion to underlying type.

Public functions

auto operator==(const Unit& other) const -> bool constexpr
Equality comparison.
auto operator!=(const Unit& other) const -> bool constexpr
Inequality comparison.
auto operator<(const Unit& other) const -> bool constexpr
Less-than comparison.
auto operator>(const Unit& other) const -> bool constexpr
Greater-than comparison.
auto operator<=(const Unit& other) const -> bool constexpr
Less-than-or-equal comparison.
auto operator>=(const Unit& other) const -> bool constexpr
Greater-than-or-equal comparison.
auto operator+(const Unit& other) const -> auto constexpr
Addition operator.
auto operator-(const Unit& other) const -> auto constexpr
Subtraction operator.
auto operator*(const Unit& other) const -> auto constexpr
Multiplication operator.
auto operator/(const Unit& other) const -> auto constexpr
Division operator.
auto operator+() const -> Unit constexpr
Unary plus operator.
auto operator-() const -> Unit constexpr
Unary minus operator.
auto operator+=(const Unit& other) -> Unit& constexpr
Compound addition assignment.
auto operator-=(const Unit& other) -> Unit& constexpr
Compound subtraction assignment.
auto operator*=(const Unit& other) -> Unit& constexpr
Compound multiplication assignment.
auto operator/=(const Unit& other) -> Unit& constexpr
Compound division assignment.

Function documentation

template<class Derived, class T>
static Derived Rodin::Math::Unit<Derived, T>::One()

Creates a unit with value 1.

Returns Unit with value $ 1 $

template<class Derived, class T>
static Derived Rodin::Math::Unit<Derived, T>::Zero()

Creates a unit with value 0.

Returns Unit with value $ 0 $

template<class Derived, class T>
Rodin::Math::Unit<Derived, T>::Unit(T v) constexpr

Constructs a unit from a value.

Parameters
in The value to wrap in the unit

template<class Derived, class T>
Rodin::Math::Unit<Derived, T>::operator T() const explicit

Explicit conversion to underlying type.

Returns The underlying value

template<class Derived, class T>
bool Rodin::Math::Unit<Derived, T>::operator==(const Unit& other) const constexpr

Equality comparison.

Parameters
other in Unit to compare with
Returns True if values are equal

template<class Derived, class T>
bool Rodin::Math::Unit<Derived, T>::operator!=(const Unit& other) const constexpr

Inequality comparison.

Parameters
other in Unit to compare with
Returns True if values are not equal

template<class Derived, class T>
bool Rodin::Math::Unit<Derived, T>::operator<(const Unit& other) const constexpr

Less-than comparison.

Parameters
other in Unit to compare with
Returns True if this value is less than other

template<class Derived, class T>
bool Rodin::Math::Unit<Derived, T>::operator>(const Unit& other) const constexpr

Greater-than comparison.

Parameters
other in Unit to compare with
Returns True if this value is greater than other

template<class Derived, class T>
bool Rodin::Math::Unit<Derived, T>::operator<=(const Unit& other) const constexpr

Less-than-or-equal comparison.

Parameters
other in Unit to compare with
Returns True if this value is less than or equal to other

template<class Derived, class T>
bool Rodin::Math::Unit<Derived, T>::operator>=(const Unit& other) const constexpr

Greater-than-or-equal comparison.

Parameters
other in Unit to compare with
Returns True if this value is greater than or equal to other

template<class Derived, class T>
auto Rodin::Math::Unit<Derived, T>::operator+(const Unit& other) const constexpr

Addition operator.

Parameters
other in Unit to add
Returns Sum of the two units

template<class Derived, class T>
auto Rodin::Math::Unit<Derived, T>::operator-(const Unit& other) const constexpr

Subtraction operator.

Parameters
other in Unit to subtract
Returns Difference of the two units

template<class Derived, class T>
auto Rodin::Math::Unit<Derived, T>::operator*(const Unit& other) const constexpr

Multiplication operator.

Parameters
other in Unit to multiply by
Returns Product of the two units

template<class Derived, class T>
auto Rodin::Math::Unit<Derived, T>::operator/(const Unit& other) const constexpr

Division operator.

Parameters
other in Unit to divide by
Returns Quotient of the two units

template<class Derived, class T>
Unit Rodin::Math::Unit<Derived, T>::operator+() const constexpr

Unary plus operator.

Returns Copy of this unit

template<class Derived, class T>
Unit Rodin::Math::Unit<Derived, T>::operator-() const constexpr

Unary minus operator.

Returns Negated unit

template<class Derived, class T>
Unit& Rodin::Math::Unit<Derived, T>::operator+=(const Unit& other) constexpr

Compound addition assignment.

Parameters
other in Unit to add
Returns Reference to this unit

template<class Derived, class T>
Unit& Rodin::Math::Unit<Derived, T>::operator-=(const Unit& other) constexpr

Compound subtraction assignment.

Parameters
other in Unit to subtract
Returns Reference to this unit

template<class Derived, class T>
Unit& Rodin::Math::Unit<Derived, T>::operator*=(const Unit& other) constexpr

Compound multiplication assignment.

Parameters
other in Unit to multiply by
Returns Reference to this unit

template<class Derived, class T>
Unit& Rodin::Math::Unit<Derived, T>::operator/=(const Unit& other) constexpr

Compound division assignment.

Parameters
other in Unit to divide by
Returns Reference to this unit