diff --git a/docs/blog/posts/isq-part-3-modeling-isq.md b/docs/blog/posts/isq-part-3-modeling-isq.md index fab9661e..fbe6d260 100644 --- a/docs/blog/posts/isq-part-3-modeling-isq.md +++ b/docs/blog/posts/isq-part-3-modeling-isq.md @@ -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` - type: + type or by calling `quantity`'s explicit constructor: ```cpp void foo(quantity q); @@ -216,6 +216,7 @@ Based on the hierarchy above, we can define the following quantity conversion ru ```cpp quantity q1 = 42 * m; quantity q2 = isq::height(q1); // explicit quantity conversion + quantity q3(q1); // direct initialization foo(isq::height(q1)); // explicit quantity conversion ``` diff --git a/docs/users_guide/framework_basics/systems_of_quantities.md b/docs/users_guide/framework_basics/systems_of_quantities.md index f5df4d82..1288c411 100644 --- a/docs/users_guide/framework_basics/systems_of_quantities.md +++ b/docs/users_guide/framework_basics/systems_of_quantities.md @@ -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)); ``` + Implicit conversions are allowed on copy-initialization: + + ```cpp + void foo(quantity q); + ``` + + ```cpp + quantity q1 = 42 * m; + quantity q2 = q1; // implicit quantity conversion + foo(q1); // implicit quantity conversion + ``` + 2. **Explicit conversions** - 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)); ``` + 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 q); + ``` + + ```cpp + quantity q1 = 42 * m; + quantity q2 = isq::height(q1); // explicit quantity conversion + quantity q3(q1); // direct initialization + foo(isq::height(q1)); // explicit quantity conversion + ``` + 3. **Explicit casts** - _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)); ``` + Explicit casts are forced with a dedicated `quantity_cast` function: + + ```cpp + void foo(quantity q); + ``` + + ```cpp + quantity q1 = 42 * m; + quantity q2 = quantity_cast(q1); // explicit quantity cast + foo(quantity_cast(q1)); // explicit quantity cast + ``` + 4. **No conversion** - _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)); ``` + Even the explicit casts will not force such a conversion: + + ```cpp + void foo(quantity); + ``` + + ```cpp + quantity q1 = 42 * s; // Compile-time error + foo(quantity_cast(42 * s)); // Compile-time error + ``` + ## Hierarchies of derived quantities