Unit Conversion and Dimensional Analysis Library 3.6.1
A compile-time, header-only C++23 dimensional-analysis library
Loading...
Searching...
No Matches
Cheat sheet

The API on one page. Every line here compiles under C++23. Assumes:

#include <units.h>
using namespace units;
using namespace units::literals;
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); // CTAD -> meters<double>
meters b = 5.0_m; // literal
auto c = 5.0 * m; // scalar * unit constant (units::m)
meters d{5.0}; // braced
meters<float> e(5.0f); // explicit representation
meters<int> f(5); // integer representation
// meters g; // needs an explicit arg: meters<double> g;
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 -> meters
feet f = m; // meters -> feet
minutes t = 90.0_s; // seconds -> minutes (value 1.5)
// meters<int> x = 1.0_ft; // ERROR: lossy into an integer representation

Arithmetic (dimensions are tracked)

square_meters area = 15.0_m * 5.0_m; // m * m -> area
meters_per_second speed = 60.0_mi / 1.0_hr; // -> velocity
auto any = 15.0_m * 5.0_m; // auto: whatever it produces (square_meters)
// meters bad = 15.0_m * 5.0_m; // ERROR: m*m is an area, not a length
// auto z = 1.0_m + 1.0_s; // ERROR: incompatible dimensions
// auto w = 1.0_m + 5.0; // ERROR: scalar + dimensioned quantity
namespace for unit types and containers representing area values

Get a plain number out

double v = d.value(); // value in the quantity's units
double r = d.raw(); // stored value (differs from value() for percent/ppm)
double t = d.to<double>(); // explicit cast to a representation
int i = d.to<int>(); // explicit, truncates toward zero
double u = unit_cast<double>(d); // equivalent to .to<double>()
// double x = d; // ERROR: no implicit unit -> double (dimensionless is the exception)
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); // converts, then compares
bool eq = (1.0_m == 100.0_cm); // true
// bool bad = (1.0_m < 1.0_kg); // ERROR: incomparable dimensions

Math (unqualified — found by ADL)

meters h = sqrt(pow<2>(3.0_m) + pow<2>(4.0_m)); // 5 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);
auto s = sin(90.0_deg); // trig needs an angle
// auto bad = sin(1.0_m); // ERROR: sin needs an angle, not a length
auto x = units::hypot(3.0_m, 4.0_m); // qualify if an unqualified call is ambiguous
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; // "5 m"
std::string s = to_string(5.0_m); // "5 m"
const char* n = (5.0_m).name(); // "meters"
const char* z = (5.0_m).abbreviation(); // "m"

Dimensionless and percent

dimensionless<double> ratio = 0.25; // double -> dimensionless (implicit)
double back = ratio; // dimensionless -> double (implicit)
auto p = 50.0_pct;
double frac = p.value(); // 0.5 (the fraction)
double pts = p.raw(); // 50 (the point count)
constexpr auto value() const noexcept
unit value
Definition core.h:2989

chrono interop

units::time::seconds<double> s = std::chrono::seconds{5}; // chrono -> unit
std::chrono::duration<double> d = 5.0_s; // unit -> chrono

Special values

auto nan = std::numeric_limits<meters<double>>::quiet_NaN();
auto inf = std::numeric_limits<meters<double>>::infinity();
bool b1 = isnan(nan); // true (unqualified, ADL)
bool b2 = isinf(inf); // true
bool b3 = isfinite(5.0_m); // true

Constrain your own templates (concepts)

template <units::UnitType U>
U twice(U x) { return x + x; } // accepts any quantity
template <units::DimensionlessUnitType U>
double as_number(U x) { return x.value(); }

Define a unit (one line)

namespace units {
UNIT_ADD(length, smoots, smoot, conversion_factor<std::ratio<17018, 10000>, meters<>>)
}
auto bridge = 364.4_smoot; // now a usable length, with its own literal
#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