Unit

Base class for units using CRTP.

Defined in Rodin/Math/Unit.h

template <
  class Derived,
  class T>
class Unit;

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: ==, !=, <, >, <=, >=

Comparisons follow the stored scalar type, including unordered floating-point NaN values.

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 types

using Type = T
The underlying value type.

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.
Unit(const Unit& other) defaulted constexpr
Copy constructor.
Unit(Unit&& other) defaulted constexpr
Move constructor.
operator T() const explicit
Explicit conversion to underlying type.

Public functions

auto operator=(const Unit& other) -> Unit& defaulted constexpr
Copy assignment operator.
auto operator=(Unit&& other) -> Unit& defaulted constexpr
Move assignment operator.
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 -> Derived constexpr
Addition operator.
auto operator-(const Unit& other) const -> Derived constexpr
Subtraction operator.
auto operator*(const Unit& other) const -> Derived constexpr
Multiplication operator.
auto operator/(const Unit& other) const -> Derived constexpr
Division operator.
auto operator+() const -> Derived constexpr
Unary plus operator.
auto operator-() const -> Derived constexpr
Unary minus operator.
auto operator+=(const Unit& other) -> Derived& constexpr
Compound addition assignment.
auto operator-=(const Unit& other) -> Derived& constexpr
Compound subtraction assignment.
auto operator*=(const Unit& other) -> Derived& constexpr
Compound multiplication assignment.
auto operator/=(const Unit& other) -> Derived& constexpr
Compound division assignment.

Function documentation

static Derived One()

Creates a unit with value 1.

Returns Unit with value $ 1 $

static Derived Zero()

Creates a unit with value 0.

Returns Unit with value $ 0 $

Unit(T v) constexpr

Constructs a unit from a value.

Parameters
v in The value to wrap in the unit

Unit(const Unit& other) defaulted constexpr

Copy constructor.

Parameters
other Object to copy from.

Unit(Unit&& other) defaulted constexpr

Move constructor.

Parameters
other Object to move from.

operator T() const explicit

Explicit conversion to underlying type.

Returns The underlying value

Unit& operator=(const Unit& other) defaulted constexpr

Copy assignment operator.

Parameters
other Object to copy from.
Returns Reference to this object after the operation.

Unit& operator=(Unit&& other) defaulted constexpr

Move assignment operator.

Parameters
other Object to move from.
Returns Reference to this object after the operation.

bool operator==(const Unit& other) const constexpr

Equality comparison.

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

bool operator!=(const Unit& other) const constexpr

Inequality comparison.

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

bool 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

bool 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

bool 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

bool 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

Derived operator+(const Unit& other) const constexpr

Addition operator.

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

Derived operator-(const Unit& other) const constexpr

Subtraction operator.

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

Derived operator*(const Unit& other) const constexpr

Multiplication operator.

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

Derived operator/(const Unit& other) const constexpr

Division operator.

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

Derived operator+() const constexpr

Unary plus operator.

Returns Copy of this unit

Derived operator-() const constexpr

Unary minus operator.

Returns Negated unit

Derived& operator+=(const Unit& other) constexpr

Compound addition assignment.

Parameters
other in Unit to add
Returns Reference to this unit

Derived& operator-=(const Unit& other) constexpr

Compound subtraction assignment.

Parameters
other in Unit to subtract
Returns Reference to this unit

Derived& operator*=(const Unit& other) constexpr

Compound multiplication assignment.

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

Derived& operator/=(const Unit& other) constexpr

Compound division assignment.

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