Support Quantity non-type template parameters #318
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Technically, we can't get non-type template parameters of custom class
type until C++20. But for a
Quantity<U, R>whose repRis 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>andQuantity<U, R>::NTTP, as long as there's an exactmatch for
UandR. In all other cases, users must use the usualQuantityconversion operators to get it into the right type. They canalso explicitly request conversion from the NTTP to the
Quantitybypassing the former to
from_nttp(...).It's a niche use case, but it's finally possible to use a
Quantityasa 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.