mirror of
https://github.com/mpusz/mp-units.git
synced 2025-06-24 16:51:33 +02:00
docs: quantity spec conversion examples updated
This commit is contained in:
@ -207,7 +207,7 @@ Based on the hierarchy above, we can define the following quantity conversion ru
|
|||||||
```
|
```
|
||||||
|
|
||||||
Explicit conversions are forced by passing the quantity to a call operator of a `quantity_spec`
|
Explicit conversions are forced by passing the quantity to a call operator of a `quantity_spec`
|
||||||
type:
|
type or by calling `quantity`'s explicit constructor:
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
void foo(quantity<isq::height[m]> q);
|
void foo(quantity<isq::height[m]> q);
|
||||||
@ -216,6 +216,7 @@ Based on the hierarchy above, we can define the following quantity conversion ru
|
|||||||
```cpp
|
```cpp
|
||||||
quantity<isq::length[m]> q1 = 42 * m;
|
quantity<isq::length[m]> q1 = 42 * m;
|
||||||
quantity<isq::height[m]> q2 = isq::height(q1); // explicit quantity conversion
|
quantity<isq::height[m]> q2 = isq::height(q1); // explicit quantity conversion
|
||||||
|
quantity<isq::height[m]> q3(q1); // direct initialization
|
||||||
foo(isq::height(q1)); // explicit quantity conversion
|
foo(isq::height(q1)); // explicit quantity conversion
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -251,6 +251,18 @@ Based on the same hierarchy of quantities of kind length, we can define quantity
|
|||||||
static_assert(implicitly_convertible(isq::radius, isq::length));
|
static_assert(implicitly_convertible(isq::radius, isq::length));
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Implicit conversions are allowed on copy-initialization:
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
void foo(quantity<isq::length[m]> q);
|
||||||
|
```
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
quantity<isq::width[m]> q1 = 42 * m;
|
||||||
|
quantity<isq::length[m]> q2 = q1; // implicit quantity conversion
|
||||||
|
foo(q1); // implicit quantity conversion
|
||||||
|
```
|
||||||
|
|
||||||
2. **Explicit conversions**
|
2. **Explicit conversions**
|
||||||
|
|
||||||
- not every _length_ is a _width_
|
- not every _length_ is a _width_
|
||||||
@ -265,6 +277,20 @@ Based on the same hierarchy of quantities of kind length, we can define quantity
|
|||||||
static_assert(explicitly_convertible(isq::length, isq::radius));
|
static_assert(explicitly_convertible(isq::length, isq::radius));
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Explicit conversions are forced by passing the quantity to a call operator of a `quantity_spec`
|
||||||
|
type or by calling `quantity`'s explicit constructor:
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
void foo(quantity<isq::height[m]> q);
|
||||||
|
```
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
quantity<isq::length[m]> q1 = 42 * m;
|
||||||
|
quantity<isq::height[m]> q2 = isq::height(q1); // explicit quantity conversion
|
||||||
|
quantity<isq::height[m]> q3(q1); // direct initialization
|
||||||
|
foo(isq::height(q1)); // explicit quantity conversion
|
||||||
|
```
|
||||||
|
|
||||||
3. **Explicit casts**
|
3. **Explicit casts**
|
||||||
|
|
||||||
- _height_ is not a _width_
|
- _height_ is not a _width_
|
||||||
@ -276,6 +302,18 @@ Based on the same hierarchy of quantities of kind length, we can define quantity
|
|||||||
static_assert(castable(isq::height, isq::width));
|
static_assert(castable(isq::height, isq::width));
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Explicit casts are forced with a dedicated `quantity_cast` function:
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
void foo(quantity<isq::height[m]> q);
|
||||||
|
```
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
quantity<isq::width[m]> q1 = 42 * m;
|
||||||
|
quantity<isq::height[m]> q2 = quantity_cast<isq::height>(q1); // explicit quantity cast
|
||||||
|
foo(quantity_cast<isq::height>(q1)); // explicit quantity cast
|
||||||
|
```
|
||||||
|
|
||||||
4. **No conversion**
|
4. **No conversion**
|
||||||
|
|
||||||
- _time_ has nothing in common with _length_
|
- _time_ has nothing in common with _length_
|
||||||
@ -286,6 +324,17 @@ Based on the same hierarchy of quantities of kind length, we can define quantity
|
|||||||
static_assert(!castable(isq::time, isq::length));
|
static_assert(!castable(isq::time, isq::length));
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Even the explicit casts will not force such a conversion:
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
void foo(quantity<isq::length[m]>);
|
||||||
|
```
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
quantity<isq::length[m]> q1 = 42 * s; // Compile-time error
|
||||||
|
foo(quantity_cast<isq::length>(42 * s)); // Compile-time error
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
## Hierarchies of derived quantities
|
## Hierarchies of derived quantities
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user