2023-06-21 10:55:18 +02:00
|
|
|
# Quick Start
|
|
|
|
|
2023-10-25 14:12:25 +02:00
|
|
|
A **quantity** is a concrete amount of a unit representing a quantity type of a specified dimension with a
|
|
|
|
specific representation. It is represented in the library with a `quantity` class template.
|
2023-06-21 10:55:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
## Creating a quantity
|
|
|
|
|
2023-09-04 10:53:47 +02:00
|
|
|
The [SI Brochure](../appendix/references.md#SIBrochure) says:
|
|
|
|
|
|
|
|
!!! quote "SI Brochure"
|
|
|
|
|
|
|
|
The value of the quantity is the product of the number and the unit. The space between the number
|
|
|
|
and the unit is regarded as a multiplication sign (just as a space between units implies
|
|
|
|
multiplication).
|
|
|
|
|
|
|
|
|
|
|
|
Following the above, the value of a quantity in the **mp-units** library is created by multiplying
|
|
|
|
a number with a predefined unit:
|
2023-06-21 10:55:18 +02:00
|
|
|
|
|
|
|
```cpp
|
|
|
|
#include <mp-units/systems/si/si.h>
|
|
|
|
|
|
|
|
using namespace mp_units;
|
|
|
|
|
2023-08-18 18:37:12 +02:00
|
|
|
quantity q = 42 * si::metre;
|
2023-06-21 10:55:18 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
!!! note
|
|
|
|
|
|
|
|
The above spelling of `metre` is not a typo. For motivation, please check our
|
2023-08-03 21:23:34 +02:00
|
|
|
[FAQ](faq.md#why-do-we-spell-metre-instead-of-meter).
|
2023-06-21 10:55:18 +02:00
|
|
|
|
|
|
|
The above creates an instance of `quantity<si::metre(), int>`. The same can be obtained using
|
|
|
|
an optional unit symbol:
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
#include <mp-units/systems/si/si.h>
|
|
|
|
|
|
|
|
using namespace mp_units;
|
|
|
|
using namespace mp_units::si::unit_symbols;
|
|
|
|
|
2023-08-18 18:37:12 +02:00
|
|
|
quantity q = 42 * m;
|
2023-06-21 10:55:18 +02:00
|
|
|
```
|
|
|
|
|
2023-08-30 11:33:30 +02:00
|
|
|
!!! tip
|
2023-06-21 10:55:18 +02:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
2023-06-29 15:09:19 +01:00
|
|
|
In case someone doesn't like the multiply syntax or there is an ambiguity between `operator*`
|
2023-11-28 11:52:37 +01:00
|
|
|
provided by this and other libraries, a quantity can also be created with a two-parameter
|
|
|
|
constructor:
|
2023-06-29 15:09:19 +01:00
|
|
|
|
|
|
|
```cpp
|
|
|
|
#include <mp-units/systems/si/si.h>
|
|
|
|
|
|
|
|
using namespace mp_units;
|
|
|
|
|
2023-11-28 11:52:37 +01:00
|
|
|
quantity q{42, si::metre};
|
2023-06-29 15:09:19 +01:00
|
|
|
```
|
|
|
|
|
2023-06-21 10:55:18 +02:00
|
|
|
|
|
|
|
## User-provided unit wrappers
|
|
|
|
|
|
|
|
Sometimes it might be awkward to type some derived units:
|
|
|
|
|
|
|
|
```cpp
|
2023-09-29 21:40:24 -06:00
|
|
|
quantity speed = 60 * km / h;
|
2023-06-21 10:55:18 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
In case such a unit is used a lot in the project, a user can easily provide a nicely named
|
|
|
|
wrapper for it with:
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
constexpr auto kmph = km / h;
|
2023-08-18 18:37:12 +02:00
|
|
|
quantity speed = 60 * kmph;
|
2023-06-21 10:55:18 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
!!! note
|
|
|
|
|
|
|
|
In case you wonder why this library does not use UDLs to create quantities, please check
|
2023-08-03 21:23:34 +02:00
|
|
|
our [FAQ](faq.md#why-dont-we-use-udls-to-create-quantities).
|