Skip to content

Conversation

@chiphogg
Copy link
Member

Technically, we can't get non-type template parameters of custom class
type until C++20. But for a Quantity<U, R> whose rep R is integral,
we can get the next best thing!

This PR introduces a public member typedef, Quantity<U, R>::NTTP,
which can be used as a template parameter, because it's an
enumeration. We support bidirectional, implicit conversion between
Quantity<U, R> and Quantity<U, R>::NTTP, as long as there's an exact
match for U and R. In all other cases, users must use the usual
Quantity conversion operators to get it into the right type. They can
also explicitly request conversion from the NTTP to the Quantity by
passing the former to from_nttp(...).

It's a niche use case, but it's finally possible to use a Quantity as
a template parameter in a unit-safe way! There just was no other
comparable solution before this.

Compile time measurements are ongoing, but so far they suggest a small
but nonzero penalty. It appears that it varies from file to file, but
is generally less than 50 ms. I think that's an OK price to pay for
this feature.

Fixes #316.

Technically, we can't get non-type template parameters of custom class
type until C++20.  But for a `Quantity<U, R>` whose rep `R` is integral,
we can get the next best thing!

This PR introduces a public member typedef, `Quantity<U, R>::NTTP`,
which _can_ be used as a template parameter, because it's an
enumeration.  We support _bidirectional, implicit_ conversion between
`Quantity<U, R>` and `Quantity<U, R>::NTTP`, as long as there's an exact
match for `U` and `R`.  In all other cases, users must use the usual
`Quantity` conversion operators to get it into the right type.  They can
also explicitly request conversion from the NTTP to the `Quantity` by
passing the former to `from_nttp(...)`.

It's a niche use case, but it's finally possible to use a `Quantity` as
a template parameter in a unit-safe way!  There just was no other
comparable solution before this.

Compile time measurements are ongoing, but so far they suggest a small
but nonzero penalty.  It appears that it varies from file to file, but
is generally less than 50 ms.  I think that's an OK price to pay for
this feature.

Fixes #316.
@chiphogg chiphogg added the release notes: ✨ lib (enhancement) PR enhancing the library code label Oct 31, 2024
@chiphogg chiphogg requested a review from geoffviola October 31, 2024 00:45
Copy link
Contributor

@geoffviola geoffviola left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. Safe, compile-time operations are great 😎

@chiphogg chiphogg merged commit f70d703 into main Oct 31, 2024
@chiphogg chiphogg deleted the chiphogg/nttp#316 branch October 31, 2024 14:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release notes: ✨ lib (enhancement) PR enhancing the library code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support NTTP for integral rep

3 participants