2023-07-11 15:40:23 +02:00
|
|
|
---
|
|
|
|
|
tags:
|
|
|
|
|
- International System
|
2023-08-30 17:42:23 +02:00
|
|
|
- Text Formatting
|
2023-07-11 15:40:23 +02:00
|
|
|
---
|
|
|
|
|
|
2023-06-22 18:22:41 +02:00
|
|
|
# `hello_units`
|
|
|
|
|
|
2023-09-13 08:58:17 +02:00
|
|
|
!!! example "[Try it on Compiler Explorer](https://godbolt.org/z/Tsesa1Pvq)"
|
2023-07-11 15:49:04 +02:00
|
|
|
|
2023-06-22 18:22:41 +02:00
|
|
|
This is a really simple example showcasing the features of the **mp-units** library.
|
|
|
|
|
|
2024-01-06 08:51:01 +01:00
|
|
|
First, we either import the `mp_units` module or include the headers for:
|
2023-06-22 18:22:41 +02:00
|
|
|
|
2024-01-06 08:51:01 +01:00
|
|
|
- an International System of Quantities (ISQ)
|
|
|
|
|
- an International System of units (SI)
|
|
|
|
|
- units derived from the International Yard and Pound
|
|
|
|
|
- text formatting and stream output support
|
2023-06-22 18:22:41 +02:00
|
|
|
|
2023-08-30 17:42:23 +02:00
|
|
|
```cpp title="hello_units.cpp" linenums="1"
|
2024-01-23 21:49:17 +01:00
|
|
|
--8<-- "example/hello_units.cpp:28:40"
|
2023-08-30 17:42:23 +02:00
|
|
|
```
|
|
|
|
|
|
2024-01-06 08:51:01 +01:00
|
|
|
Also, to shorten the definitions, we "import" all the symbols from the `mp_units` namespace.
|
2023-08-30 17:42:23 +02:00
|
|
|
|
2024-01-23 21:49:17 +01:00
|
|
|
```cpp title="hello_units.cpp" linenums="13"
|
|
|
|
|
--8<-- "example/hello_units.cpp:41:42"
|
2023-06-22 18:22:41 +02:00
|
|
|
```
|
|
|
|
|
|
2024-01-06 08:51:01 +01:00
|
|
|
Next, we define a simple function that calculates the average speed based on the provided
|
2023-08-30 17:42:23 +02:00
|
|
|
arguments of length and time:
|
2023-06-22 18:22:41 +02:00
|
|
|
|
2024-01-23 21:49:17 +01:00
|
|
|
```cpp title="hello_units.cpp" linenums="14"
|
|
|
|
|
--8<-- "example/hello_units.cpp:43:46"
|
2023-06-22 18:22:41 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
The above function template takes any quantities implicitly convertible to `isq::length`
|
2024-01-06 08:51:01 +01:00
|
|
|
and `isq::time`, respectively. Those quantities can use any compatible unit and a
|
|
|
|
|
representation type. The function returns a result of a straightforward equation and ensures
|
2023-08-30 17:42:23 +02:00
|
|
|
that its quantity type is implicitly convertible to `isq::speed`.
|
2023-06-22 18:22:41 +02:00
|
|
|
|
2023-08-30 17:42:23 +02:00
|
|
|
!!! tip
|
2023-06-22 18:22:41 +02:00
|
|
|
|
|
|
|
|
Besides verifying the type returned from the function, constraining a generic return
|
2024-01-06 08:51:01 +01:00
|
|
|
type is beneficial for users of such a function as it provides more information
|
2023-06-22 18:22:41 +02:00
|
|
|
of what to expect from a function than just using `auto`.
|
|
|
|
|
|
2024-01-23 21:49:17 +01:00
|
|
|
```cpp title="hello_units.cpp" linenums="18"
|
|
|
|
|
--8<-- "example/hello_units.cpp:48:51"
|
2023-06-22 18:22:41 +02:00
|
|
|
```
|
|
|
|
|
|
2024-01-06 08:51:01 +01:00
|
|
|
The above lines explicitly opt into using unit symbols from two systems of units.
|
2023-06-22 18:22:41 +02:00
|
|
|
As this introduces a lot of short identifiers into the current scope, it is not done
|
|
|
|
|
implicitly while including a header file.
|
|
|
|
|
|
2024-01-23 21:49:17 +01:00
|
|
|
```cpp title="hello_units.cpp" linenums="22"
|
|
|
|
|
--8<-- "example/hello_units.cpp:53:59"
|
2023-06-22 18:22:41 +02:00
|
|
|
```
|
|
|
|
|
|
2024-01-06 08:51:01 +01:00
|
|
|
- Lines `21` & `22` create a quantity of kind `isq::length / isq::time` with the numbers
|
2023-06-22 18:22:41 +02:00
|
|
|
and units provided. Such quantities can be converted or assigned to any other quantity
|
|
|
|
|
with a matching kind.
|
2024-01-06 08:51:01 +01:00
|
|
|
- Line `23` calls our function template with quantities of kind `isq::length` and
|
2023-06-22 18:22:41 +02:00
|
|
|
`isq::time` and number and units provided.
|
2024-01-06 08:51:01 +01:00
|
|
|
- Line `24` explicitly provides quantity types of the quantities passed to a function template.
|
|
|
|
|
This time, those will not be quantity kinds anymore and will have
|
2023-08-30 17:42:23 +02:00
|
|
|
[more restrictive conversion rules](../framework_basics/simple_and_typed_quantities.md#quantity_cast-to-force-unsafe-conversions).
|
2024-01-06 08:51:01 +01:00
|
|
|
- Line `25` changes the unit of a quantity `v3` to `m / s` in a
|
2023-08-30 17:42:23 +02:00
|
|
|
[value-preserving way](../framework_basics/value_conversions.md#value-preserving-conversions)
|
2023-06-22 18:22:41 +02:00
|
|
|
(floating-point representations are considered to be value-preserving).
|
2024-01-06 08:51:01 +01:00
|
|
|
- Line `26` does a similar operation, but this time, it would also succeed for
|
2023-08-30 17:42:23 +02:00
|
|
|
[value-truncating cases](../framework_basics/value_conversions.md#value-truncating-conversions)
|
2024-01-06 08:51:01 +01:00
|
|
|
(if that was the case).
|
|
|
|
|
- Line `27` does a [value-truncating conversion](../framework_basics/value_conversions.md#value-truncating-conversions)
|
2023-08-30 17:42:23 +02:00
|
|
|
of changing the underlying representation type from `double` to `int`.
|
2023-06-22 18:22:41 +02:00
|
|
|
|
2024-01-23 21:49:17 +01:00
|
|
|
```cpp title="hello_units.cpp" linenums="29"
|
|
|
|
|
--8<-- "example/hello_units.cpp:61"
|
2023-06-22 18:22:41 +02:00
|
|
|
```
|
|
|
|
|
|
2023-08-30 17:42:23 +02:00
|
|
|
The above presents [various ways to print a quantity](../framework_basics/text_output.md).
|
2024-01-06 08:51:01 +01:00
|
|
|
Both stream insertion operations and `std::format` facilities are supported.
|
2023-06-22 18:22:41 +02:00
|
|
|
|
2023-08-30 17:42:23 +02:00
|
|
|
!!! tip
|
2023-06-22 18:22:41 +02:00
|
|
|
|
2024-01-06 08:51:01 +01:00
|
|
|
`MP_UNITS_STD_FMT` is used for compatibility reasons. If a specific compiler
|
|
|
|
|
does not support `std::format` or a user prefers to use the `{fmt}` library, this macro
|
|
|
|
|
will resolve to `fmt` namespace. Otherwise, the `std` namespace will be used.
|