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

Refactor get_value<T>(m) to enable success query #183

Merged
merged 2 commits into from
Oct 24, 2023

Conversation

chiphogg
Copy link
Contributor

Right now, get_value<T>(m) for a magnitude m either produces the
requested value, or invokes a hard compiler error via static_assert.
It would be nice if we had a way to ask whether this operation would
succeed. For this purpose, we now provide representable_in<T>(m).

It turns out that we can't actually answer this question without
effectively doing all of the work to compute the value. We won't want
to do this twice, because that would uselessly slow down compile times.
Therefore, we extract all of our logic into a common implementation
function. The logic is basically equivalent to the pre-existing version
of get_value<T>(m), except that instead of each static_cast, we
return a unique value of a new enum we created for this purpose.

This means that each function can get its result efficiently. For
get_value, we change the trigger for each static_assert to the
corresponding enum value. For representable_in, we simply check
whether that enum has the OK value.

This PR helps pave the way for #90. When we make our Constant class
implicitly convertible to Quantity types, it turns out that we can do
better than the "overflow safety surface"... because we know the exact
value! We can achieve perfect compile-time checking. This PR will give
us a single function that we can call in our implementation for that.

Includes docs and tests.

Right now, `get_value<T>(m)` for a magnitude `m` either produces the
requested value, or invokes a hard compiler error via `static_assert`.
It would be nice if we had a way to ask whether this operation would
succeed.  For this purpose, we now provide `representable_in<T>(m)`.

It turns out that we can't actually answer this question without
effectively doing all of the work to compute the value.  We won't want
to do this twice, because that would uselessly slow down compile times.
Therefore, we extract all of our logic into a common implementation
function.  The logic is basically equivalent to the pre-existing version
of `get_value<T>(m)`, except that instead of each `static_cast`, we
return a unique value of a new `enum` we created for this purpose.

This means that each function can get its result efficiently.  For
`get_value`, we change the trigger for each `static_assert` to the
corresponding enum value.  For `representable_in`, we simply check
whether that enum has the `OK` value.

Includes docs and tests.
@chiphogg chiphogg added the release notes: ✨ lib (enhancement) PR enhancing the library code label Oct 21, 2023
docs/reference/magnitude.md Outdated Show resolved Hide resolved
Co-authored-by: Geoffrey Viola <[email protected]>
@chiphogg chiphogg merged commit bfd4ee7 into main Oct 24, 2023
10 checks passed
@chiphogg chiphogg deleted the chiphogg/representable-in#90 branch October 24, 2023 12:04
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.

2 participants