Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
Look into mp-units. Here is what code that does what the question mentions looks like: #include <print> // or import std; #include <mp-units/systems/si.h> // or import mp_units; ...
#1: Initial revision
Look into [mp-units](https://mpusz.github.io/mp-units/latest/).
Here is what code that does what the question mentions looks like:
```c++
#include <print>
// or import std;
#include <mp-units/systems/si.h>
// or import mp_units;
auto main() -> int
{
using namespace mp_units::si::unit_symbols;
constexpr auto v1 = 2 * km;
constexpr auto v2 = 3 * m;
std::println("Result is: {}", v1 + v2);
}
```
Output:
```text
Result is: 2003 m
```
You can actually play around with it yourself, because it’s one of the libraries Compiler Explorer knows about: https://compiler-explorer.com/z/YfcqPTMjK
I haven’t used mp-units in a major project yet, but I have used it in a half-dozen or so one-off programs to solve specific problems, mostly just to test it out. My experience has been pretty positive.
I mainly started using it because, in my work, there is a *lot* of jumping back-and-forth between SI and US-customary units; I have been burned with mixing units before. I have written small, quick-and-dirty-solution programs that use flow rates, mass, time, and temperature, and one time with density and volume. Not complex programs—you could solve the calculations on the back of an envelope—but coding it was handy for allowing values to be tweaked to see what would happen. (And also it was an excuse to take mp-units for a test run.)
I haven’t tried out any of the *really* complex features of the library. But what I did use was pleasant.
About a decade or so ago I tried Boost.Units… didn’t thrill me, never really went back to it. As I recall, the biggest headache was dealing with quantities relative to a base. Haven’t tried that with mp-units yet, though.
The main reason I’ve been experimenting with mp-units is because [it is on track for standardization](https://wg21.link/p3045).
