A C++14-compatible physical units library with no dependencies and a single-file delivery option. Emphasis on safety, accessibility, performance, and developer experience.
Apache License 2.0
329
stars
21
forks
source link
Support Quantity non-type template parameters #318
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.
Technically, we can't get non-type template parameters of custom class type until C++20. But for a
Quantity<U, R>
whose repR
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 betweenQuantity<U, R>
andQuantity<U, R>::NTTP
, as long as there's an exact match forU
andR
. In all other cases, users must use the usualQuantity
conversion operators to get it into the right type. They can also explicitly request conversion from the NTTP to theQuantity
by passing the former tofrom_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.