Literals, Factory Functions, and Type Aliases

The units library provides three complementary ways to construct and work with physical quantities: literal operators, factory functions, and type aliases.

Literal Operators

User-defined literal operators for all SI units with their prefixes. Considering all combinations of underlying types, prefixes, and units, there are more than 50,000 literals defined.

Naming convention

  • All literals start with _ (e.g. 1_m, 2.5_s)

  • SI prefixes are prepended (e.g. 1_km, 1_mm, 1_µm)

  • Floating-point type suffix: _f (float), none or _d (double), _l (long double)

  • Negative exponents use _ as a separator (e.g. _m_s for m/s, _m_s2 for m/s²)

SI prefixes

Prefix Symbol Factor

quecto

q

10-30

ronto

r

10-27

yocto

y

10-24

zepto

z

10-21

atto

a

10-18

femto

f

10-15

pico

p

10-12

nano

n

10-9

micro

µ

10-6

milli

m

10-3

centi

c

10-2

deci

d

10-1

deca

da

101

hecto

h

102

kilo

k

103

mega

M

106

giga

G

109

tera

T

1012

peta

P

1015

exa

E

1018

Factory Functions

Factory functions provide the same functionality as literal operators but with a function call syntax. They are named as plurals or descriptive names.

Examples
auto d = nin::centimetres(3.0);   // 3 cm
auto t = nin::seconds(1.5);      // 1.5 s
auto v = nin::metres_v<float>(2.0f);  // 2 m as float

Common factory functions

Function Returns

seconds(), seconds_v<T>()

time (second)

metres(), metres_v<T>()

length (metre)

grams(), grams_v<T>()

mass (gram)

amperes(), amperes_v<T>()

electric current (ampere)

kelvins(), kelvins_v<T>()

temperature (kelvin)

mols(), mols_v<T>()

amount of substance (mole)

candelas(), candelas_v<T>()

luminous intensity (candela)

newtons(), pascals(), joules(), watts()

derived SI units

minutes(), hours(), days()

common time units

degrees(), arcminutes(), arcseconds()

angle units

inches(), feet(), yards(), miles()

imperial length

Type Aliases

For each physical quantity, the library provides template and concrete aliases.

Pattern

Pattern Example

name_v<T>

length_v<float> — template alias for any floating-point type

name

length — alias for name_v<double>

name_f

length_f — alias for name_v<float>

name_d

length_d — alias for name_v<double>

name_l

length_l — alias for name_v<long double>

Common type aliases

Alias Unit Dimension

time

second

T

length

metre

L

mass

kilogram

M

electric_current

ampere

I

temperature

kelvin

Θ

amount_of_substance

mole

N

luminous_intensity

candela

J

angle

radian

(dimensionless)

frequency

hertz

T-1

force

newton

M·L·T-2

pressure

pascal

M·L-1·T-2

energy

joule

M·L2·T-2

power

watt

M·L2·T-3

electric_charge

coulomb

T·I

voltage

volt

M·L2·T-3·I-1