Skip to content

Commit 93f3be4

Browse files
committed
docs: quantity spec conversion examples updated
1 parent 9c140f4 commit 93f3be4

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

docs/blog/posts/isq-part-3-modeling-isq.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ Based on the hierarchy above, we can define the following quantity conversion ru
207207
```
208208

209209
Explicit conversions are forced by passing the quantity to a call operator of a `quantity_spec`
210-
type:
210+
type or by calling `quantity`'s explicit constructor:
211211

212212
```cpp
213213
void foo(quantity<isq::height[m]> q);
@@ -216,6 +216,7 @@ Based on the hierarchy above, we can define the following quantity conversion ru
216216
```cpp
217217
quantity<isq::length[m]> q1 = 42 * m;
218218
quantity<isq::height[m]> q2 = isq::height(q1); // explicit quantity conversion
219+
quantity<isq::height[m]> q3(q1); // direct initialization
219220
foo(isq::height(q1)); // explicit quantity conversion
220221
```
221222

docs/users_guide/framework_basics/systems_of_quantities.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,18 @@ Based on the same hierarchy of quantities of kind length, we can define quantity
251251
static_assert(implicitly_convertible(isq::radius, isq::length));
252252
```
253253

254+
Implicit conversions are allowed on copy-initialization:
255+
256+
```cpp
257+
void foo(quantity<isq::length[m]> q);
258+
```
259+
260+
```cpp
261+
quantity<isq::width[m]> q1 = 42 * m;
262+
quantity<isq::length[m]> q2 = q1; // implicit quantity conversion
263+
foo(q1); // implicit quantity conversion
264+
```
265+
254266
2. **Explicit conversions**
255267

256268
- not every _length_ is a _width_
@@ -265,6 +277,20 @@ Based on the same hierarchy of quantities of kind length, we can define quantity
265277
static_assert(explicitly_convertible(isq::length, isq::radius));
266278
```
267279

280+
Explicit conversions are forced by passing the quantity to a call operator of a `quantity_spec`
281+
type or by calling `quantity`'s explicit constructor:
282+
283+
```cpp
284+
void foo(quantity<isq::height[m]> q);
285+
```
286+
287+
```cpp
288+
quantity<isq::length[m]> q1 = 42 * m;
289+
quantity<isq::height[m]> q2 = isq::height(q1); // explicit quantity conversion
290+
quantity<isq::height[m]> q3(q1); // direct initialization
291+
foo(isq::height(q1)); // explicit quantity conversion
292+
```
293+
268294
3. **Explicit casts**
269295

270296
- _height_ is not a _width_
@@ -276,6 +302,18 @@ Based on the same hierarchy of quantities of kind length, we can define quantity
276302
static_assert(castable(isq::height, isq::width));
277303
```
278304

305+
Explicit casts are forced with a dedicated `quantity_cast` function:
306+
307+
```cpp
308+
void foo(quantity<isq::height[m]> q);
309+
```
310+
311+
```cpp
312+
quantity<isq::width[m]> q1 = 42 * m;
313+
quantity<isq::height[m]> q2 = quantity_cast<isq::height>(q1); // explicit quantity cast
314+
foo(quantity_cast<isq::height>(q1)); // explicit quantity cast
315+
```
316+
279317
4. **No conversion**
280318

281319
- _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
286324
static_assert(!castable(isq::time, isq::length));
287325
```
288326

327+
Even the explicit casts will not force such a conversion:
328+
329+
```cpp
330+
void foo(quantity<isq::length[m]>);
331+
```
332+
333+
```cpp
334+
quantity<isq::length[m]> q1 = 42 * s; // Compile-time error
335+
foo(quantity_cast<isq::length>(42 * s)); // Compile-time error
336+
```
337+
289338

290339
## Hierarchies of derived quantities
291340

0 commit comments

Comments
 (0)