feat: convertibility of a quantity with a unit one with the raw value added

Resolves #553
This commit is contained in:
Mateusz Pusz
2024-07-14 18:31:11 +02:00
parent 0dfd323c4b
commit fcc16ae282
5 changed files with 191 additions and 11 deletions

View File

@@ -24,7 +24,7 @@ Here is a small example of operations possible on scalar quantities:
static_assert(2 * m * (3 * m) == 6 * m2);
static_assert(10 * km / (5 * km) == 2 * one);
static_assert(10 * km / (5 * km) == 2);
static_assert(1000 / (1 * s) == 1 * kHz);
```
@@ -51,7 +51,7 @@ Here is a small example of operations possible on scalar quantities:
static_assert(2 * m * (3 * m) == 6 * m2);
static_assert(10 * km / (5 * km) == 2 * one);
static_assert(10 * km / (5 * km) == 2);
static_assert(1000 / (1 * s) == 1 * kHz);
```

View File

@@ -164,6 +164,21 @@ inline constexpr struct parts_per_million final : named_unit<"ppm", mag_ratio<1,
inline constexpr auto ppm = parts_per_million;
```
### Superpowers of the unit `one`
Quantities of the unit `one` are the only ones that are implicitly convertible from a raw value
and explicitly convertible to it. This property also expands to usual arithmetic operators.
Thanks to the above, we can type:
```cpp
quantity<one> inc(quantity<one> q) { return q + 1; }
void legacy(double) { /* ... */ }
if (auto q = inc(42); q != 0)
legacy(static_cast<int>(q));
```
## Angular quantities