r/cpp • u/mateusz_pusz • Mar 26 '26
P3054 - Quantities and Units Library
Today I delivered an Evening Session to the ISO C++ Committee on the Quantities and Units Library, which is under consideration for standardization as part of C++29. Nothing is sure for now, but fingers crossed đ¤
If you want to learn more about the proposal itself, please check the paper P3045. However, I really like the slides I presented today, so I decided to share them with you immediately as well. You can find them in my GitHub repo. Please review them, try the workshops (just click the QR code in the corner), experiment, and share feedback.
5
u/James20k P2005R0 Mar 27 '26
Its nice to see that this supports irrational units! That's a common omission
I do wonder if there's ever going to be a way to support types with numerically defined exponents in these libraries (I have a constant of type of kg1-Î m3Î-1 s-2 ), but its a very tricky problem - and not a critique of this proposal
3
u/TheoreticalDumbass :illuminati: Mar 27 '26
what if a unit was redefined, how would future evolution of this handle it? the metre has been redefined 6-ish times i think? https://en.wikipedia.org/wiki/Metre
as in, if a unit was redefined and ratio to a different unit changed
3
u/Morwenn Mar 27 '26
Hello, the slides are nice, but correct me if I'm wrong: they essentially propose to standardize the V2 of mp-units, right? Is the plan to standardize the V2, or to upgrade it all and standardize V3 with the absolute/delta/point split?
3
u/Plazmatic Mar 27 '26 edited Mar 27 '26
Excellent presentation. However, while I'm not against mp-units eventually joining the standard, and maybe it will fix these stupid Clang and MSVC ICEs and compiler regressions that keep popping up around it (not caused by it, but exposed by it) if this gets standardized, mp-units has a long way to go before I'd feel comfortable with it being in the standard.
For example, I still don't think mp-units handles things like bearing angle vs azimuthal angle vs elevation angle, and those things are currently just completely un-representable within mp-units. There needs to be some change like how temperatures were handled for that to work, like how you've got different zero points. I should be able to add radians to an azimuthal radian/gradian/cycle/degree but not use them interchangeably with one another with out an conversion (but this conversion does not need to be explicit anymore than a conversion between Km and M would be), since it isn't straight forward with azimuthal/elevation/bearing angles and operations between themselves.
I think 167 gets close to dealing with this (we can create two different point of origins for each of these types of angles) but there's no modulo arithmetic AFAIK, so that kind of makes things weird, for example what happens when you make an elevation angle go past it's peak? Additionally doing this affine space business means you can't use plain
x * radjust like howdeg_Cisn't a thing, which I don't think is quite right either (you'd never assume the domain of an angle from the fact that it's an angle period, it would always be assumed to be the equivalent of a delta).This is further ironic with the MacCreadyAlititude example on pages 32 and 37, like yeah, it sucks they aren't using units, but this actually isn't solvable by the current mp-units design either. I'd still have to make my own library to deal with the different angle domain problem.
Unlike some of the niche scenarios, this directly effects HAE,LLA, other geographic position types etc... which means this will be a very common pain point.
Then you've got the whole "is a pixel a unit" problem which causes some weird ambiguities because (correct me if I'm wrong) it's currently not possible to correctly represent something like that AFAIK, if I multiply (10 * pixel) and (20 * pixel) together, I should get (200 * pixel) not 200 * pixel2.
3
u/YouNeedDoughnuts Mar 27 '26
This is great. I've seen SI/Imperial conversion errors, radians vs degrees errors, formula derivation errors which would have been caught with unit checking, and missed scaling implications in optimisation functions. Using suffixes requires discipline, is hard to reason about for complicated equations, and is still prone to human error.
Will it be possible to have a vector with heterogenous units? That's very central in robotics where rotary and linear actuator variables are generalised together.
2
u/QQII Mar 27 '26
Iâve just got to the section on âQuantity Safetyâ. Itâs really clearing up a gripe Iâve always had with unit libraries in the past where as your example shows
  1Hz + 1Bq + 1Bq = 2s-1
Iâve got some questions on the Box example:
More broadly, how do quantities work in practice? If width, length and height werenât private then you could perform arithmetic across two widths from different boxes. To enforce distinctness, youâd have to resort to getters and setters or give each box a distinct quantity (is this possible?), right?
2
u/Lengador Mar 28 '26
Coming from industry and having looked at unit libraries before, the first hurdle was compile times. The two units I tried at the time (can't remember how long ago now) blew up compile times enough in a simple project that they were immediately discarded.
The relative origin points look fantastic for a common use case I find, where velocities or accelerations are measured at different points on a rigid body and angular velocity is not accounted for (for example, the velocity of a GNSS antenna vs an INS). That is a major thing which has been missing from unit libraries for a long time. I'm definitely going to look at using mp_units again now that I know it has that functionality.
2
u/CornedBee Mar 30 '26
Really awesome work on this. My main project is a very heavy user of Boost.Units, and switching to mp-units is somewhere in the future - now that I know v3 is coming and can see the new ideas, I will probably put this off until v3 is usable.
7
u/drphillycheesesteak Mar 26 '26
Are you worried about an fmt or range-v3 situation with your library, where by the time itâs in the standard, you have had to make compromises and pare things down to the point where the std version isnât useful for advanced use cases anymore?
12
u/a_falsity Mar 27 '26
Just wanted to say this looks awesome. Currently doing everything with doubles/integers and hoping the suffixes on variable names prevent bugs. Looks like it could be used to add a lot of safety to coordinate conversion libraries (thinking LLA/ECEF/RAE).
2
u/TheoreticalDumbass :illuminati: Mar 27 '26
i am not against this (in fact this wouldve probably been useful for me in some graphics code with angle manipulations), just wondering, as this is super useful for certification, why dont the certification bodies maintain something like this?
also, it would be nice if a trimmed slide pdf was provided, only last slide of each page shown ("Certification requirements" page is spread over 5 slides), current form makes sense for presentation, but is unnecessarily wide for later reading
12
u/fdwr fdwr@github đ Mar 27 '26 edited Mar 27 '26
Thank you for working to prevent a repeat of the Mars Climate Orbiter issue.
Section 17.1 has a nice little sample snippet of usage:
(đ¤ now I'm thinking of adding unit suffixes to a little DSL of mine, so people can freely mix
16bits + 3byteswithout worry)4
u/FrancoisCarouge Mar 27 '26
Excellent slides!
4
u/matthieum Mar 27 '26
Yes, Yes, YES!
I'm so glad to finally see this point being taken into consideration.
(Well, that and affine space, because scalar != point != vector)
This is the first proposal for a units library that is really getting me excited. I really appreciate how you made it very simple to understand the 6 levels of differentiation in the slides.
2
u/ContDiArco Mar 27 '26
Excelent structured and aproachable.
Thank you!
1
u/tmlnz Mar 30 '26
I was wondering where the idea of a quantities and units library in C++ standard came from? Did it come from extending the std::chrono types, or from one of the surveys about possible new features in reddit/isocpp.org years ago?
0
u/ATownHoldItDown Mar 27 '26
This is a rather trivial complaint, but your syntax highlighting on your practical examples REALLY threw me for a loop at first. I'll try to approximate how my brain perceived it using reddit's formatting...
What this asserts in plain terms:
How my brain interpreted your syntax highlighting at first:
I think your justification for the library is accurate. I think the proposed change is good overall. But I'm not sure I am on board with the way the syntax is implemented. Great effort overall.