Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider providing constant instead of named_unit for constants #677

Open
mpusz opened this issue Feb 11, 2025 · 5 comments
Open

Consider providing constant instead of named_unit for constants #677

mpusz opened this issue Feb 11, 2025 · 5 comments
Labels
design Design-related discussion
Milestone

Comments

@mpusz
Copy link
Owner

mpusz commented Feb 11, 2025

Such constants could have superpowers. They could implicitly convert to a quantity and provide additional compile-time overflow protection as discussed in #669_

One possible problem is to decide which representation type should be used in case we want to allow CTAD (e.g., quantity{si::standard_gravity}). Forcing std::arithmetic types might be a no-go for some projects that use safe numeric wrappers. This is actually where the current unit-based approach shines. The constant is orthogonal to the representation type in a quantity.

@mpusz mpusz added the design Design-related discussion label Feb 11, 2025
@mpusz mpusz added this to the v2.5.0 milestone Feb 11, 2025
@chiphogg
Copy link
Collaborator

One possible problem is to decide which representation type should be used in case we want to allow CTAD (e.g., quantity{si::standard_gravity}).

From my point of view, the right answer is straight forward: "we don't want to allow CTAD".

The mp-units-ification of the corresponding Au interfaces would probably look something like si::standard_gravity.in<T>(), creating a quantity whose rep is T, and whose unit is standard gravity (thus, holding a value of T{1}). And we could also have si::standard_gravity.in<T>(m/s/s) if we wanted a different unit --- and this operation would have the perfect conversion policy that you mentioned in the original post.

@mpusz
Copy link
Owner Author

mpusz commented Feb 11, 2025

Well, I did not plan to add any fancy interface (like .in<T>(Unit)) to constants to not complicate the design.

I thought that maybe being able to pass si::standard_gravity to quantity<isq::acceleration[m/s2]> would be enough.

Then I realized that we encourage people to use generic programming with concepts (e.g., QuantityOf<isq::acceleration> auto) which would not force the conversion and will not match the concept (the same as we discussed for Zero). This is why I thought that we may want to pass quantity{si::standard_gravity} in such cases, but this would require CTAD to work.

@chiphogg
Copy link
Collaborator

Yeah, it's on me to write a doc to explain the concept of "shapeshifter types". These are monovalue types that can "shapeshift" to become any type from some family of types. Zero and Constant are the classic examples, where the family of types is Quantity<U, R> (speaking "Au language" for the duration of this comment).

The "superpower" of a shapeshifter type is that it enables effortless comparisons, additions, subtractions, assignments, etc. with any one type from that family of types. You can simply "compare to ZERO", or "assign from SPEED_OF_LIGHT", and it will automatically generate the most efficient and correct code. This is a great benefit for expressibility!

Their weakness --- their "kryptonite", if you will --- is that you can only use them in a context where it's clear which type they need to become, which is not true for generic interfaces.

So, bringing a shapeshifter type to a generic interface is simply an incorrect usage. Once we have good explanations of shapeshifter types as a concept* that we can link to, then the community can learn that this simply isn't how to use them. And then we can get the benefits of the use cases where they do apply.

I am hoping to give a talk on Monovalue Types in CppCon 2025, and "shapeshifter types" will be a key component if I do.

Anyway: I really do think CTAD is the wrong direction to go here, because there's no way to get it without privileging some choice of rep above all others, and I want to avoid doing that.

*"As a concept": concept in the generic sense, not the C++20 sense, naturally.

@JohelEGP
Copy link
Collaborator

Why not give them the reference treatment, so that quantity{0, si::standard_gravity} works (note the 0 for an int rep)?

@mpusz
Copy link
Owner Author

mpusz commented Feb 11, 2025

They actually work as a reference now, and we can type 1 * si::standard_gravity, which is much shorter ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
design Design-related discussion
Projects
None yet
Development

No branches or pull requests

3 participants