The API on one page. Every line here compiles under C++23. Assumes:
namespace for unit literal definitions of all categories.
Unit Conversion Library namespace.
Definition units.h:106
Complete implementation of units - a compile-time, header-only, unit conversion library built on c++2...
Make a quantity
meters a(5.0);
meters b = 5.0_m;
meters d{5.0};
meters<int> f(5);
constexpr coulombs e(1.602176634e-19)
elementary charge.
constexpr meters_per_second c(299792458.0)
Speed of light in vacuum.
Literal representation follows the decimal point: 5_m is meters<int>, 5.0_m is meters<double>.
Convert (implicit, lossless only)
meters m = 100.0_ft;
feet f = m;
minutes t = 90.0_s;
Arithmetic (dimensions are tracked)
square_meters
area = 15.0_m * 5.0_m;
meters_per_second speed = 60.0_mi / 1.0_hr;
auto any = 15.0_m * 5.0_m;
namespace for unit types and containers representing area values
Get a plain number out
double v = d.value();
double r = d.raw();
double t = d.to<double>();
int i = d.to<int>();
constexpr T unit_cast(const Unit &value) noexcept
Casts an unit to an arithmetic type.
Definition core.h:3754
Compare
bool lt = (1.0_m < 2.0_ft);
bool eq = (1.0_m == 100.0_cm);
Math (unqualified — found by ADL)
meters
h = sqrt(pow<2>(3.0_m) + pow<2>(4.0_m));
meters mn = min(1.0_m, 2.0_ft);
meters mx = max(1.0_m, 2.0_ft);
meters ab = abs(-3.0_m);
dimensionless< detail::floating_point_promotion_t< typename AngleUnit::underlying_type > > sin(const AngleUnit angle) noexcept
Compute sine.
Definition angle.h:105
constexpr detail::floating_point_promotion_t< std::common_type_t< UnitTypeLhs, UnitTypeRhs > > hypot(const UnitTypeLhs &x, const UnitTypeRhs &y)
Computes the square root of the sum-of-squares of x and y.
Definition core.h:5385
constexpr unit< compound_conversion_factor< joules_, seconds_ > > h(6.62607015e-34)
Planck constant.
Name, print, serialize
std::cout << 5.0_m;
std::string s = to_string(5.0_m);
const char* n = (5.0_m).name();
const char* z = (5.0_m).abbreviation();
Dimensionless and percent
dimensionless<double> ratio = 0.25;
double back = ratio;
auto p = 50.0_pct;
double pts = p.raw();
constexpr auto value() const noexcept
unit value
Definition core.h:2989
chrono interop
units::time::seconds<double> s = std::chrono::seconds{5};
std::chrono::duration<double> d = 5.0_s;
Special values
auto nan = std::numeric_limits<meters<double>>::quiet_NaN();
auto inf = std::numeric_limits<meters<double>>::infinity();
bool b1 = isnan(nan);
bool b2 = isinf(inf);
bool b3 = isfinite(5.0_m);
Constrain your own templates (concepts)
template <units::UnitType U>
U twice(U x) { return x + x; }
template <units::DimensionlessUnitType U>
double as_number(U x) { return x.value(); }
Define a unit (one line)
}
auto bridge = 364.4_smoot;
#define UNIT_ADD(namespaceName, namePlural, abbreviation,...)
Macro for generating the boilerplate code needed for a new unit.
Definition core.h:447
namespace for unit types and containers representing length values
Type representing an arbitrary conversion factor between units.
Definition core.h:1563
See defining new units for prefixes and compound units.
References