Permit arithmetic operations with more types #229
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.
The goal of these
std::is_arithmetic
SFINAE uses was to avoidambiguous overloads when we multiply a
Quantity
with anotherQuantity
. It was only ever a shortcut. It seems to work well in mostcases, but recently, a user pointed out that it doesn't help
std::complex
. (To be clear, we could form aQuantity
withstd::complex
rep, but we couldn't multiply or divide aQuantity
witha
std::complex
scalar.)This PR takes a different approach.
First, we define what constitutes a valid "rep". We're very permissive
here, using a "deny-list" approach that filters out empty types, known
Au types, and other libraries' units types (which we detect via our
CorrespondingQuantity
trait). Incidentally, this should give us anice head start on #52.
Next, we permit an operation (multiplication or division) depending on
how the "candidate scalar" type would interact with the Quantity's rep:
simply put, the result must itself both exist, and be a valid rep.
The net result should be that we automatically consider a much wider
variety of types --- including complex number types from outside the
standard library --- to be valid scalars. Crucially, this should also
be able to avoid breaking existing code: we are introducing potentially
a lot of new overloads for these operators, and we can't allow any to
create an ambiguity with existing overloads.
Helps #228, but we need more Magnitude support.