mirror of
https://github.com/mpusz/mp-units.git
synced 2025-08-05 13:14:29 +02:00
docs: "C++ modules" tabs added to all the code examples
This commit is contained in:
@@ -2,6 +2,35 @@
|
|||||||
|
|
||||||
Here is a small example of operations possible on scalar quantities:
|
Here is a small example of operations possible on scalar quantities:
|
||||||
|
|
||||||
|
=== "C++ modules"
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
import mp_units;
|
||||||
|
|
||||||
|
using namespace mp_units;
|
||||||
|
using namespace mp_units::si::unit_symbols;
|
||||||
|
|
||||||
|
// simple numeric operations
|
||||||
|
static_assert(10 * km / 2 == 5 * km);
|
||||||
|
|
||||||
|
// unit conversions
|
||||||
|
static_assert(1 * h == 3600 * s);
|
||||||
|
static_assert(1 * km + 1 * m == 1001 * m);
|
||||||
|
|
||||||
|
// derived quantities
|
||||||
|
static_assert(1 * km / (1 * s) == 1000 * m / s);
|
||||||
|
static_assert(2 * km / h * (2 * h) == 4 * km);
|
||||||
|
static_assert(2 * km / (2 * km / h) == 1 * h);
|
||||||
|
|
||||||
|
static_assert(2 * m * (3 * m) == 6 * m2);
|
||||||
|
|
||||||
|
static_assert(10 * km / (5 * km) == 2 * one);
|
||||||
|
|
||||||
|
static_assert(1000 / (1 * s) == 1 * kHz);
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Header files"
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
#include <mp-units/systems/si/si.h>
|
#include <mp-units/systems/si/si.h>
|
||||||
|
|
||||||
@@ -37,6 +66,45 @@ performed without sacrificing accuracy. Please see the below example for a quick
|
|||||||
|
|
||||||
*[NTTP]: Non-Type Template Parameter
|
*[NTTP]: Non-Type Template Parameter
|
||||||
|
|
||||||
|
=== "C++ modules"
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
#include <iostream>
|
||||||
|
import mp_units;
|
||||||
|
|
||||||
|
using namespace mp_units;
|
||||||
|
|
||||||
|
constexpr QuantityOf<isq::speed> auto avg_speed(QuantityOf<isq::length> auto d,
|
||||||
|
QuantityOf<isq::time> auto t)
|
||||||
|
{
|
||||||
|
return d / t;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
using namespace mp_units::si::unit_symbols;
|
||||||
|
using namespace mp_units::international::unit_symbols;
|
||||||
|
|
||||||
|
constexpr quantity v1 = 110 * km / h;
|
||||||
|
constexpr quantity v2 = 70 * mph;
|
||||||
|
constexpr quantity v3 = avg_speed(220. * isq::distance[km], 2 * h);
|
||||||
|
constexpr quantity v4 = avg_speed(isq::distance(140. * mi), 2 * h);
|
||||||
|
constexpr quantity v5 = v3.in(m / s);
|
||||||
|
constexpr quantity v6 = value_cast<m / s>(v4);
|
||||||
|
constexpr quantity v7 = value_cast<int>(v6);
|
||||||
|
|
||||||
|
std::cout << v1 << '\n'; // 110 km/h
|
||||||
|
std::cout << v2 << '\n'; // 70 mi/h
|
||||||
|
std::cout << std::format("{}", v3) << '\n'; // 110 km/h
|
||||||
|
std::cout << std::format("{:*^14}", v4) << '\n'; // ***70 mi/h****
|
||||||
|
std::cout << std::format("{:%Q in %q}", v5) << '\n'; // 30.5556 in m/s
|
||||||
|
std::cout << std::format("{0:%Q} in {0:%q}", v6) << '\n'; // 31.2928 in m/s
|
||||||
|
std::cout << std::format("{:%Q}", v7) << '\n'; // 31
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Header files"
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
#include <mp-units/format.h>
|
#include <mp-units/format.h>
|
||||||
#include <mp-units/ostream.h>
|
#include <mp-units/ostream.h>
|
||||||
|
@@ -20,6 +20,18 @@ The [SI Brochure](../appendix/references.md#SIBrochure) says:
|
|||||||
Following the above, the value of a quantity in the **mp-units** library is created by multiplying
|
Following the above, the value of a quantity in the **mp-units** library is created by multiplying
|
||||||
a number with a predefined unit:
|
a number with a predefined unit:
|
||||||
|
|
||||||
|
=== "C++ modules"
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
import mp_units;
|
||||||
|
|
||||||
|
using namespace mp_units;
|
||||||
|
|
||||||
|
quantity q = 42 * si::metre / si::second;
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Header files"
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
#include <mp-units/systems/si/si.h>
|
#include <mp-units/systems/si/si.h>
|
||||||
|
|
||||||
@@ -34,6 +46,18 @@ quantity q = 42 * si::metre / si::second;
|
|||||||
provided by this and other libraries, a quantity can also be created with a two-parameter
|
provided by this and other libraries, a quantity can also be created with a two-parameter
|
||||||
constructor:
|
constructor:
|
||||||
|
|
||||||
|
=== "C++ modules"
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
import mp_units;
|
||||||
|
|
||||||
|
using namespace mp_units;
|
||||||
|
|
||||||
|
quantity q{42, si::metre / si::second};
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Header files"
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
#include <mp-units/systems/si/si.h>
|
#include <mp-units/systems/si/si.h>
|
||||||
|
|
||||||
@@ -45,6 +69,19 @@ quantity q = 42 * si::metre / si::second;
|
|||||||
The above creates an instance of `quantity<derived_unit<si::metre, per<si::second>>{}, int>`.
|
The above creates an instance of `quantity<derived_unit<si::metre, per<si::second>>{}, int>`.
|
||||||
The same can be obtained using optional unit symbols:
|
The same can be obtained using optional unit symbols:
|
||||||
|
|
||||||
|
=== "C++ modules"
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
import mp_units;
|
||||||
|
|
||||||
|
using namespace mp_units;
|
||||||
|
using namespace mp_units::si::unit_symbols;
|
||||||
|
|
||||||
|
quantity q = 42 * m / s;
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Header files"
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
#include <mp-units/systems/si/si.h>
|
#include <mp-units/systems/si/si.h>
|
||||||
|
|
||||||
@@ -62,6 +99,19 @@ quantity q = 42 * m / s;
|
|||||||
|
|
||||||
Quantities of the same kind can be added, subtracted, and compared to each other:
|
Quantities of the same kind can be added, subtracted, and compared to each other:
|
||||||
|
|
||||||
|
=== "C++ modules"
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
import mp_units;
|
||||||
|
|
||||||
|
using namespace mp_units;
|
||||||
|
using namespace mp_units::si::unit_symbols;
|
||||||
|
|
||||||
|
static_assert(1 * km + 50 * m == 1050 * m);
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Header files"
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
#include <mp-units/systems/si/si.h>
|
#include <mp-units/systems/si/si.h>
|
||||||
|
|
||||||
@@ -96,6 +146,27 @@ Quantity points should be used in all places where adding two values is meaningl
|
|||||||
The set of operations that can be done on quantity points is limited compared to quantities.
|
The set of operations that can be done on quantity points is limited compared to quantities.
|
||||||
This introduces an additional type-safety.
|
This introduces an additional type-safety.
|
||||||
|
|
||||||
|
=== "C++ modules"
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
#include <iostream>
|
||||||
|
import mp_units;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
using namespace mp_units;
|
||||||
|
using namespace mp_units::si::unit_symbols;
|
||||||
|
using namespace mp_units::usc::unit_symbols;
|
||||||
|
|
||||||
|
quantity_point temp{20. * deg_C};
|
||||||
|
std::cout << "Temperature: "
|
||||||
|
<< temp.quantity_from_zero() << " ("
|
||||||
|
<< temp.in(deg_F).quantity_from_zero() << ")\n";
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Header files"
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
#include <mp-units/ostream.h>
|
#include <mp-units/ostream.h>
|
||||||
#include <mp-units/systems/si/si.h>
|
#include <mp-units/systems/si/si.h>
|
||||||
|
@@ -38,6 +38,24 @@ The library source code is hosted on [GitHub](https://github.com/mpusz/mp-units)
|
|||||||
More requirements for C++ modules support can be found in the
|
More requirements for C++ modules support can be found in the
|
||||||
[CMake's documentation](https://cmake.org/cmake/help/latest/manual/cmake-cxxmodules.7.html).
|
[CMake's documentation](https://cmake.org/cmake/help/latest/manual/cmake-cxxmodules.7.html).
|
||||||
|
|
||||||
|
=== "C++ modules"
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
#include <iostream>
|
||||||
|
import mp_units;
|
||||||
|
|
||||||
|
using namespace mp_units;
|
||||||
|
|
||||||
|
inline constexpr struct smoot : named_unit<"smoot", mag<67> * usc::inch> {} smoot;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
constexpr quantity dist = 364.4 * smoot;
|
||||||
|
std::cout << "Harvard Bridge length = " << dist << "(" << dist.in(usc::foot) << ", " << dist.in(si::metre) << ") ± 1 εar\n";
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Header files"
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
#include <mp-units/ostream.h>
|
#include <mp-units/ostream.h>
|
||||||
|
@@ -53,6 +53,36 @@ have shorter type identifiers, resulting in easier-to-understand error messages
|
|||||||
|
|
||||||
Here is a simple example showing how to deal with such quantities:
|
Here is a simple example showing how to deal with such quantities:
|
||||||
|
|
||||||
|
=== "C++ modules"
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
#include <iostream>
|
||||||
|
import mp_units;
|
||||||
|
|
||||||
|
using namespace mp_units;
|
||||||
|
|
||||||
|
constexpr quantity<si::metre / si::second> avg_speed(quantity<si::metre> d,
|
||||||
|
quantity<si::second> t)
|
||||||
|
{
|
||||||
|
return d / t;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
using namespace mp_units::si::unit_symbols;
|
||||||
|
|
||||||
|
const quantity distance = 110 * km;
|
||||||
|
const quantity duration = 2 * h;
|
||||||
|
const quantity speed = avg_speed(distance, duration);
|
||||||
|
|
||||||
|
std::cout << "A car driving " << distance << " in " << duration
|
||||||
|
<< " has an average speed of " << speed
|
||||||
|
<< " (" << speed.in(km / h) << ")\n";
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Header files"
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
#include <mp-units/ostream.h>
|
#include <mp-units/ostream.h>
|
||||||
#include <mp-units/systems/si/si.h>
|
#include <mp-units/systems/si/si.h>
|
||||||
@@ -144,9 +174,38 @@ accident.
|
|||||||
|
|
||||||
The previous example can be re-typed using typed quantities in the following way:
|
The previous example can be re-typed using typed quantities in the following way:
|
||||||
|
|
||||||
|
=== "C++ modules"
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
#include <iostream>
|
||||||
|
import mp_units;
|
||||||
|
|
||||||
|
using namespace mp_units;
|
||||||
|
using namespace mp_units::si::unit_symbols;
|
||||||
|
|
||||||
|
constexpr quantity<isq::speed[m / s]> avg_speed(quantity<isq::length[m]> d,
|
||||||
|
quantity<isq::time[s]> t)
|
||||||
|
{
|
||||||
|
return d / t;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
const quantity distance = isq::distance(110 * km);
|
||||||
|
const quantity duration = isq::time(2 * h);
|
||||||
|
const quantity speed = avg_speed(distance, duration);
|
||||||
|
|
||||||
|
std::cout << "A car driving " << distance << " in " << duration
|
||||||
|
<< " has an average speed of " << speed
|
||||||
|
<< " (" << speed.in(km / h) << ")\n";
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Header files"
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
#include <mp-units/ostream.h>
|
#include <mp-units/ostream.h>
|
||||||
#include <mp-units/systems/isq/space_and_time.h>
|
#include <mp-units/systems/isq/isq.h>
|
||||||
#include <mp-units/systems/si/si.h>
|
#include <mp-units/systems/si/si.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
@@ -175,7 +234,7 @@ int main()
|
|||||||
A car driving 110 km in 2 h has an average speed of 15.2778 m/s (55 km/h)
|
A car driving 110 km in 2 h has an average speed of 15.2778 m/s (55 km/h)
|
||||||
```
|
```
|
||||||
|
|
||||||
!!! example "[Try it on Compiler Explorer](https://godbolt.org/z/q3PzMzqsh)"
|
!!! example "[Try it on Compiler Explorer](https://godbolt.org/z/MWxG1j4Pc)"
|
||||||
|
|
||||||
In case we will accidentally make the same calculation error as before, this time, we will
|
In case we will accidentally make the same calculation error as before, this time, we will
|
||||||
get a bit longer error message, this time also containing information about the quantity type:
|
get a bit longer error message, this time also containing information about the quantity type:
|
||||||
@@ -200,6 +259,118 @@ but there are scenarios where they offer additional level of safety.
|
|||||||
|
|
||||||
Let's see another example:
|
Let's see another example:
|
||||||
|
|
||||||
|
=== "C++ modules"
|
||||||
|
|
||||||
|
=== "Simple"
|
||||||
|
|
||||||
|
```cpp hl_lines="41"
|
||||||
|
#include <numbers>
|
||||||
|
import mp_units;
|
||||||
|
|
||||||
|
using namespace mp_units;
|
||||||
|
|
||||||
|
class StorageTank {
|
||||||
|
quantity<square(si::metre)> base_;
|
||||||
|
quantity<si::metre> height_;
|
||||||
|
public:
|
||||||
|
constexpr StorageTank(const quantity<square(si::metre)>& base,
|
||||||
|
const quantity<si::metre>& height) :
|
||||||
|
base_(base), height_(height)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
// ...
|
||||||
|
};
|
||||||
|
|
||||||
|
class CylindricalStorageTank : public StorageTank {
|
||||||
|
public:
|
||||||
|
constexpr CylindricalStorageTank(const quantity<si::metre>& radius,
|
||||||
|
const quantity<si::metre>& height) :
|
||||||
|
StorageTank(std::numbers::pi * pow<2>(radius), height)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class RectangularStorageTank : public StorageTank {
|
||||||
|
public:
|
||||||
|
constexpr RectangularStorageTank(const quantity<si::metre>& length,
|
||||||
|
const quantity<si::metre>& width,
|
||||||
|
const quantity<si::metre>& height) :
|
||||||
|
StorageTank(length * width, height)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
using namespace mp_units::si::unit_symbols;
|
||||||
|
auto tank = RectangularStorageTank(1'000 * mm, 500 * mm, 200 * mm);
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Typed"
|
||||||
|
|
||||||
|
```cpp hl_lines="51 52 53"
|
||||||
|
#include <numbers>
|
||||||
|
import mp_units;
|
||||||
|
|
||||||
|
using namespace mp_units;
|
||||||
|
using namespace mp_units::si::unit_symbols;
|
||||||
|
|
||||||
|
// add a custom quantity type of kind isq::length
|
||||||
|
inline constexpr struct horizontal_length
|
||||||
|
: quantity_spec<isq::length> {} horizontal_length;
|
||||||
|
|
||||||
|
// add a custom derived quantity type of kind isq::area
|
||||||
|
// with a constrained quantity equation
|
||||||
|
inline constexpr struct horizontal_area
|
||||||
|
: quantity_spec<isq::area, horizontal_length * isq::width> {} horizontal_area;
|
||||||
|
|
||||||
|
class StorageTank {
|
||||||
|
quantity<horizontal_area[m2]> base_;
|
||||||
|
quantity<isq::height[m]> height_;
|
||||||
|
public:
|
||||||
|
constexpr StorageTank(const quantity<horizontal_area[m2]>& base,
|
||||||
|
const quantity<isq::height[m]>& height) :
|
||||||
|
base_(base), height_(height)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
// ...
|
||||||
|
};
|
||||||
|
|
||||||
|
class CylindricalStorageTank : public StorageTank {
|
||||||
|
public:
|
||||||
|
constexpr CylindricalStorageTank(const quantity<isq::radius[m]>& radius,
|
||||||
|
const quantity<isq::height[m]>& height) :
|
||||||
|
StorageTank(quantity_cast<horizontal_area>(std::numbers::pi * pow<2>(radius)),
|
||||||
|
height)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class RectangularStorageTank : public StorageTank {
|
||||||
|
public:
|
||||||
|
constexpr RectangularStorageTank(const quantity<horizontal_length[m]>& length,
|
||||||
|
const quantity<isq::width[m]>& width,
|
||||||
|
const quantity<isq::height[m]>& height) :
|
||||||
|
StorageTank(length * width, height)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
auto tank = RectangularStorageTank(horizontal_length(1'000 * mm),
|
||||||
|
isq::width(500 * mm),
|
||||||
|
isq::height(200 * mm));
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Header files"
|
||||||
|
|
||||||
=== "Simple"
|
=== "Simple"
|
||||||
|
|
||||||
```cpp hl_lines="42"
|
```cpp hl_lines="42"
|
||||||
@@ -253,7 +424,7 @@ Let's see another example:
|
|||||||
|
|
||||||
```cpp hl_lines="53 54 55"
|
```cpp hl_lines="53 54 55"
|
||||||
#include <mp-units/math.h>
|
#include <mp-units/math.h>
|
||||||
#include <mp-units/systems/isq/space_and_time.h>
|
#include <mp-units/systems/isq/isq.h>
|
||||||
#include <mp-units/systems/si/si.h>
|
#include <mp-units/systems/si/si.h>
|
||||||
#include <numbers>
|
#include <numbers>
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user