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

Permit arithmetic operations with more types #229

Merged
merged 1 commit into from
Apr 25, 2024

Conversation

chiphogg
Copy link
Contributor

@chiphogg chiphogg commented Mar 30, 2024

The goal of these std::is_arithmetic SFINAE uses was to avoid
ambiguous overloads when we multiply a Quantity with another
Quantity. It was only ever a shortcut. It seems to work well in most
cases, but recently, a user pointed out that it doesn't help
std::complex. (To be clear, we could form a Quantity with
std::complex rep, but we couldn't multiply or divide a Quantity with
a 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 a
nice 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.

The goal of these `std::is_arithmetic` SFINAE uses was to avoid
ambiguous overloads when we multiply a `Quantity` with another
`Quantity`.  It was only ever a shortcut.  It seems to work well in most
cases, but recently, a user pointed out that it doesn't help
`std::complex`.  (To be clear, we could _form_ a `Quantity` with
`std::complex` rep, but we couldn't multiply or divide a `Quantity` with
a `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 a
nice 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.
@chiphogg chiphogg added the release notes: 🐛 lib (bugfix) PR fixing a defect in the library code label Mar 30, 2024
@chiphogg chiphogg marked this pull request as ready for review April 24, 2024 18:14
Copy link
Contributor

@geoffviola geoffviola left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementations and tests look good. Thanks!

@chiphogg chiphogg merged commit 87bf5b3 into main Apr 25, 2024
10 checks passed
@chiphogg chiphogg deleted the chiphogg/enable-if-not-quantity#228 branch April 25, 2024 14:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release notes: 🐛 lib (bugfix) PR fixing a defect in the library code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants