Image

Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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.

Any testimonials for any C++ units of measure library?

+5
−0

The question is about libraries that extend the data type system to ensure physically realistic computations. Think std::chrono but for distance and mass and other things as well as for time. Instead of adding 2 and 3 to get five, you can add 2 kilometers and 3 meters to get 2003 meters. It's easy to find people who dislike using them, but are there success stories? My particular reservations are about learning curves. I'm in a shop with a smart but very small team that can't take a lot of time to figure out something like Boost Units. That leaves, by one estimate, about 3700 other libraries in the same space. If anyone's a satisfied user, which one did you integrate in your project, how did you pick it, and what problems should I look out for?

History

1 comment thread

What problem does it solve? (3 comments)

2 answers

+1
−0

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;

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:

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.

History

0 comment threads

+1
−0

Looking for testimonials, so I'm going to self-promote.

I had similar problems at work, and didn't like their homegrown solution. Well I mostly liked it, but I had some issues with it.

Mostly, I didn't like that I had to add multiple lines just to add a new unit. So I started working on an alternative and published what I have on github.

https://github.com/bensaboff/CPP-Units-Conversion

I would like some feedback anyway, so if you'd like to look at it, please do so and let me know what you think.

It's still a bit of a work in progress, but I'm looking to start using it as is.

History

1 comment thread

First, immediate feedback: `README.md` is missing. (1 comment)

Sign up to answer this question »