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:
- Multiplication/division: ,
- Unary operations: ,
- 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
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
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-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) 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 |