#include <Rodin/Math/Unit.h>
template<class Derived, class T>
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:
- Multiplication/division: ,
- Unary operations: ,
- 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
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>
Rodin:: Math:: Unit<Derived, T>:: Unit(T v) constexpr
Constructs a unit from a value.
| Parameters | |
|---|---|
| v 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-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>
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 |