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

Support mixed operators with other strong typedefs and noncommutative mixed operators #75

Conversation

gerboengels
Copy link
Contributor

The first commit enables 'mixed' operators with other strong_typedefs, by applying the operator on the underlying type of both strong_typedefs.

The second commits adds noncommutative mixed operators, useful for subtraction and division.

Use case demonstrating both cases:

struct year_offset : strong_typedef<year_offset, int> {};
struct year : strong_typedef<year, int>,
              mixed_subtraction_noncommutative<year, year_offset> {};
year y2018(2018);
year_offset o(3);
year y2015 = y2018 - offset; // OK, creates a year-object by subtracting the underlying values of both strong_typedefs
offset - y2018; // compile error, operator not found.

@gerboengels gerboengels force-pushed the support_mixed_operators_with_other_strong_typedefs_and_noncommutative_mixed_operators branch 2 times, most recently from 77de364 to 9a10835 Compare March 28, 2018 13:22
Mixed operators, such as mixed_addition, work fine when the underlying type
and the other type are compatible.
But when the other type is a strong_typedef, and they are not convertible
(which is the whole point of using a strong_typedef), mixed operators do
not work.
This commit checks whether the other type is a strong typedef, and if so,
applies the operator on the underlying type, instead of the strong typedef
itself.

Two use cases:
- Mixing a strong typedef for some value (such as 'year', like 2018) and some
offset related to such values (such as 'year_offset', like +3). These types
should not be implicitly convertible, but you do want 'year' to support
mixed addition with 'year_offset' on the underlying (int) type.
- Mixing an amount (euro's) strong typedef with a percentage strong typedef.
These types are obviously not convertible, but
mixed_multiplication<amount, percentage> on the underlying doubles is desirable
@gerboengels gerboengels force-pushed the support_mixed_operators_with_other_strong_typedefs_and_noncommutative_mixed_operators branch from 9a10835 to e2aa33a Compare March 28, 2018 14:22
Subtraction and division are noncommutative ((a - b) != (b - a)), and when mixing
different types you might not want these operators to work both ways.
This commit adds mixed_[operator]_noncommutative, which does _not_ declare the
operator overloads with the strong_typedef as the right hand side and the
other (mixed) type as the left hand side

Example:

struct year : strong_typedef<year, int>,
              mixed_subtraction_noncommutative<year, int> {};
year y(2018);
y - 3; // OK
3 - y; // compile error, operator not found.
@gerboengels gerboengels force-pushed the support_mixed_operators_with_other_strong_typedefs_and_noncommutative_mixed_operators branch from e2aa33a to c198661 Compare March 29, 2018 15:13
@JohelEGP
Copy link
Contributor

Github accepted a review I did even though there had been a forced push which fixed what I wanted to point out. I only just realized that I could cancel the review.

This addition looks good to me, and I'll certainly use it when defining quantity types.

@foonathan foonathan merged commit 5275797 into foonathan:master Apr 2, 2018
@foonathan
Copy link
Owner

I agree, thank's for doing that.

saraedum pushed a commit to saraedum/type_safe that referenced this pull request Jul 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants