Files
mp-units/docs/getting_started/quick_start.md

1.9 KiB

Quick Start

A quantity is a concrete amount of a unit for a quantity type of a specified dimension with a specific representation, and is represented in the library with a quantity class template.

Creating a quantity

The quantity is created by multiplying a number with a predefined unit:

#include <mp-units/systems/si/si.h>

using namespace mp_units;

auto q = 42 * si::metre;

!!! note

The above spelling of `metre` is not a typo. For motivation, please check our
[FAQ](../faq/#why-do-we-spell-metre-instead-of-meter).

The above creates an instance of quantity<si::metre(), int>. The same can be obtained using an optional unit symbol:

#include <mp-units/systems/si/si.h>

using namespace mp_units;
using namespace mp_units::si::unit_symbols;

auto q = 42 * m;

!!! note

Unit symbols introduce a lot of short identifiers into the current namespace, and that is
why they are opt-in. A user has to explicitly "import" them from a dedicated `unit_symbols`
namespace.

User-provided unit wrappers

Sometimes it might be awkward to type some derived units:

auto speed = 60 * (km / h);

!!! note

Please note that `60 * km / h` will not compile. To read more about the rationale for such
a design please check our [FAQ](../faq/#why-dont-we-use-udls-to-create-a-quantity).

In case such a unit is used a lot in the project, a user can easily provide a nicely named wrapper for it with:

constexpr auto kmph = km / h;
auto speed = 60 * kmph;

or even:

constexpr auto kilometre = si::kilo<si::metre>;
constexpr auto kilometre_per_hour = kilometre / si::hour;
constexpr auto kmph = kilometre_per_hour;
auto speed = 60 * kmph;

!!! note

In case you wonder why this library does not use UDLs to create quantities, please check
our [FAQ](../faq/#why-dont-we-use-udls-to-create-quantities).