Skip to content

Conversation

@chiphogg
Copy link
Member

We add special types for the kinds of operations that can occur in unit
conversions. Later on, we'll create traits that judge the risks of
these operations for overflow and truncation, and can even determine
which individual values will be lossy in these ways.

For now, the operations are:

  • StaticCast<T, U> represents a static cast from type T to U.
  • MultiplyTypeBy<T, M> represents multiplying a value of T by the
    magnitude M.
  • DivideTypeByInteger<T, M> represents dividing a value of T by the
    magnitude M, which must be an integer.
  • OpSequence<Ops...> chains any sequence of operations, and is
    automatically flattened to a single layer.

We also provide the OpInput<Op> and OpOutput<Op> traits. For
OpSequence, we also explicitly check that the output type of one
operation equals the input type of the next. This has helped prevent a
few errors already.

Helps #349.

We add special types for the kinds of operations that can occur in unit
conversions.  Later on, we'll create traits that judge the risks of
these operations for overflow and truncation, and can even determine
which individual values will be lossy in these ways.

For now, the operations are:

- `StaticCast<T, U>` represents a static cast from type `T` to `U`.
- `MultiplyTypeBy<T, M>` represents multiplying a value of `T` by the
  magnitude `M`.
- `DivideTypeByInteger<T, M>` represents dividing a value of `T` by the
  magnitude `M`, which must be an integer.
- `OpSequence<Ops...>` chains any sequence of operations, and is
  automatically flattened to a single layer.

We also provide the `OpInput<Op>` and `OpOutput<Op>` traits.  For
`OpSequence`, we also explicitly check that the output type of one
operation equals the input type of the next.  This has helped prevent a
few errors already.

Helps #349.
Comment on lines +78 to +81
TEST(DivideTypeByInteger, IntegerTypeDividedByIntegerTooBigToRepresentGivesZero) {
EXPECT_THAT((DivideTypeByInteger<uint8_t, decltype(mag<256>())>::apply_to(uint8_t{1})),
SameTypeAndValue(uint8_t{0}));
}
Copy link
Member

Choose a reason for hiding this comment

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

Would it make sense for an additional case that tests dividing to a result of 0 without overflowing the result type?

TEST(DivideTypeByInteger, IntegerTypeDividedByIntegerMagGreaterThanDividend) {
    EXPECT_THAT((DivideTypeByInteger<uint8_t, decltype(mag<2>())>::apply_to(uint8_t{1})),
                SameTypeAndValue(uint8_t{0}));
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Done, thanks!

@chiphogg chiphogg merged commit 6bc9161 into main Jul 10, 2025
14 checks passed
@chiphogg chiphogg deleted the chiphogg/abstract-ops#349 branch July 10, 2025 15:55
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