2020-05-05 20:57:58 +02:00
|
|
|
[](./LICENSE.md)
|
2019-05-08 09:52:46 -06:00
|
|
|
[](https://travis-ci.com/mpusz/units)
|
2020-05-17 12:07:09 +02:00
|
|
|
[](https://bintray.com/mpusz/conan-mpusz/mp-units%3Ampusz/0.5.0%3Astable/link)
|
2019-10-04 08:29:24 +02:00
|
|
|
[](https://bintray.com/mpusz/conan-mpusz/mp-units%3Ampusz/_latestVersion)
|
2019-12-23 13:25:49 +01:00
|
|
|
|
2019-09-17 06:46:10 -06:00
|
|
|
# `mp-units` - A Units Library for C++
|
2018-08-22 12:11:19 +02:00
|
|
|
|
2020-03-10 13:33:11 +01:00
|
|
|
**The mp-units library is the subject of this ISO C++ paper: [P1935](https://wg21.link/p1935).
|
|
|
|
|
It is explained in this [CppCon 2019 talk](https://youtu.be/0YW6yxkdhlU) (slightly dated now).
|
|
|
|
|
We are working towards potentially having it standardized for C++23 and are actively looking
|
|
|
|
|
for parties interested in field trialing the library.**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Documentation
|
|
|
|
|
|
|
|
|
|
An extensive project documentation including installation instructions and user's
|
|
|
|
|
guide can be found on [mp-units GitHub Pages](https://mpusz.github.io/units).
|
|
|
|
|
|
|
|
|
|
|
2019-12-17 09:15:31 +01:00
|
|
|
## TL;DR
|
|
|
|
|
|
2020-03-09 18:55:41 +01:00
|
|
|
`mp-units` is a compile-time enabled Modern C++ library that provides compile-time dimensional
|
|
|
|
|
analysis and unit/quantity manipulation. The basic idea and design heavily bases on
|
|
|
|
|
`std::chrono::duration` and extends it to work properly with many dimensions.
|
|
|
|
|
|
|
|
|
|
Here is a small example of possible operations:
|
|
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
|
// simple numeric operations
|
2020-09-09 19:20:35 +02:00
|
|
|
static_assert(10_q_km / 2 == 5_q_km);
|
2020-03-09 18:55:41 +01:00
|
|
|
|
|
|
|
|
// unit conversions
|
2020-09-09 19:20:35 +02:00
|
|
|
static_assert(1_q_h == 3600_q_s);
|
|
|
|
|
static_assert(1_q_km + 1_q_m == 1001_q_m);
|
2020-03-09 18:55:41 +01:00
|
|
|
|
|
|
|
|
// dimension conversions
|
2020-09-09 19:20:35 +02:00
|
|
|
static_assert(1_q_km / 1_q_s == 1000_q_m_per_s);
|
|
|
|
|
static_assert(2_q_km_per_h * 2_q_h == 4_q_km);
|
|
|
|
|
static_assert(2_q_km / 2_q_km_per_h == 1_q_h);
|
2020-06-25 17:45:11 +02:00
|
|
|
|
2020-09-09 19:20:35 +02:00
|
|
|
static_assert(2_q_m * 3_q_m == 6_q_m2);
|
2020-06-25 17:45:11 +02:00
|
|
|
|
2020-09-09 19:20:35 +02:00
|
|
|
static_assert(10_q_km / 5_q_km == 2);
|
2020-06-25 17:45:11 +02:00
|
|
|
|
2020-09-09 19:20:35 +02:00
|
|
|
static_assert(1000 / 1_q_s == 1_q_kHz);
|
2020-03-09 18:55:41 +01:00
|
|
|
```
|
|
|
|
|
|
2020-05-11 08:52:08 +02:00
|
|
|
_Try it on the [Compiler Explorer](https://godbolt.org/z/XPmjPz)._
|
2020-03-09 18:55:41 +01:00
|
|
|
|
|
|
|
|
This library requires some C++20 features (concepts, classes as NTTPs, ...). Thanks to
|
|
|
|
|
them the user gets a powerful but still easy to use interface and all unit conversions
|
|
|
|
|
and dimensional analysis can be performed without sacrificing on accuracy. Please see
|
|
|
|
|
the below example for a quick preview of basic library features:
|
2020-02-15 21:17:52 +00:00
|
|
|
|
2019-12-17 09:15:31 +01:00
|
|
|
```cpp
|
2020-05-10 17:31:47 +02:00
|
|
|
#include <units/physical/si/speed.h>
|
|
|
|
|
#include <units/physical/international/speed.h>
|
2020-09-07 12:45:56 +02:00
|
|
|
#include <units/format.h>
|
2019-12-17 10:12:17 +01:00
|
|
|
#include <iostream>
|
|
|
|
|
|
2020-05-08 22:39:24 +02:00
|
|
|
using namespace units::physical;
|
2019-12-17 10:12:17 +01:00
|
|
|
|
2020-05-10 17:31:47 +02:00
|
|
|
constexpr Speed auto avg_speed(Length auto d, Time auto t)
|
2019-12-17 09:15:31 +01:00
|
|
|
{
|
|
|
|
|
return d / t;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
2020-05-08 22:39:24 +02:00
|
|
|
using namespace units::physical::si::literals;
|
2020-09-09 19:20:35 +02:00
|
|
|
Speed auto v1 = avg_speed(220_q_km, 2_q_h);
|
2020-05-10 17:31:47 +02:00
|
|
|
Speed auto v2 = avg_speed(si::length<international::mile>(140), si::time<si::hour>(2));
|
|
|
|
|
Speed auto v3 = quantity_cast<si::metre_per_second>(v2);
|
|
|
|
|
Speed auto v4 = quantity_cast<int>(v3);
|
2019-12-17 09:15:31 +01:00
|
|
|
|
2020-09-07 12:45:56 +02:00
|
|
|
std::cout << v1 << '\n'; // 110 km/h
|
|
|
|
|
std::cout << fmt::format("{}", v2) << '\n'; // 70 mi/h
|
|
|
|
|
std::cout << fmt::format("{:%Q in %q}", v3) << '\n'; // 31.2928 in m/s
|
|
|
|
|
std::cout << fmt::format("{:%Q}", v4) << '\n'; // 31
|
2019-12-17 09:15:31 +01:00
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2020-09-07 12:45:56 +02:00
|
|
|
_Try it on the [Compiler Explorer](https://godbolt.org/z/xE91TY)._
|