nholthaus / units

a compile-time, header-only, dimensional analysis and unit conversion library built on c++14 with no dependencies.
http://nholthaus.github.io/units/
MIT License
938 stars 134 forks source link

Feature Request: Allow defining incompatible units using existing unit_type/dimensions #281

Open stellarpower opened 2 years ago

stellarpower commented 2 years ago

Afraid I'm new to this library, so apologies if this has already been fixed/covered in another issue or I'm not getting things techinically correct.

I see an example here with something similar to what I am lookingto use the library for. I will have co-ordinates in the real world, and co-ordinates expressed in terms of a screen - in pixels, naturally - and I have code that maps between the two. I want to ensure that erroneous conversions can't happen.

I see that v3.0 will introduce the ability to add new base units, which is great. However, I'd like to add that I'd argue pixels are still in the dimensions of length. We can do all sorts of things the same - area, volume, accelerations - but, crucially, we shouldn't be able to convert (at leats not off the bat) betwee pixels and metres, because they represent units in totally different abstract co-ordinate spaces. It may be overkill to add a lot of user-defined unit types when we're really representing the same physical concepts but in a different world.

To add another couple of example use cases:

So whilst I can define some new units to suit my use-case of pixels, I think that it might also be helpful if it were possible to define new base_unit sets that are using the same existing dimensions, but are not convertible between each other - we can't simply represent one as a std::ratio of the other. I'd be concerned that adding new dimensions repeatedly could rapidly lead to code bloat if the library is well-utilised in a body of code, where I'd prefer to see new dimensions added when the quantity being expressed is totally different from anything already in the standard set - money, as an example. I could probably achieve this by adding some new base units and strong-type wrapping these, but I think having some of this built-in, as a halfway between a new dimension and using existing units would be a bonus. Even just tag dispatch added in might be enough.

Thanks :)

JohelEGP commented 2 years ago

I could probably achieve this by adding some new base units and strong-type wrapping these

I think a separate base dimension does the job.