Refactor get_value<T>(m)
to enable success query
#183
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.
Right now,
get_value<T>(m)
for a magnitudem
either produces therequested 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 eachstatic_cast
, wereturn 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 eachstatic_assert
to thecorresponding enum value. For
representable_in
, we simply checkwhether that enum has the
OK
value.This PR helps pave the way for #90. When we make our
Constant
classimplicitly convertible to
Quantity
types, it turns out that we can dobetter 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.