Skip to content

Commit

Permalink
Add the dividable_by and dividable_to traits
Browse files Browse the repository at this point in the history
  • Loading branch information
doom committed Aug 4, 2019
1 parent 74d4292 commit c21788a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ The table below describes the built-in traits that can be applied to a given str
| `multiplicable` | Two `T` objects can be multiplied to obtain a new `T`. |
| `multiplicable_with<U>` | A `T` object can be multiplied with a `U` object to obtain a new `T`. |
| `dividable` | A `T` object can be divided by another `T` object to obtain a new `T`. |
| `dividable_by<U>` | A `T` object can be divided by a `U` object to obtain a new `T`. |
| `dividable_to<U>` | A `T` object can be divided by another `T` object to obtain a new `U`. |
| `modulable` | A `T` object can be moduled from another `T` object to obtain a new `T`. |
| `incrementable` | A `T` object can be pre-incremented and post-incremented. |
| `decrementable` | A `T` object can be pre-decremented and post-decremented. |
Expand Down
14 changes: 14 additions & 0 deletions include/st/traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,20 @@ namespace st
using type = traits::dividable<T>;
};

template <typename OtherOperandT>
struct dividable_by
{
template <typename T>
using type = traits::dividable<T, OtherOperandT>;
};

template <typename ReturnT>
struct dividable_to
{
template <typename T>
using type = traits::dividable<T, T, ReturnT>;
};

struct modulable
{
template <typename T>
Expand Down
8 changes: 7 additions & 1 deletion tests/strong_type-tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ using speed = st::type<
struct speed_tag,
st::addable,
st::subtractable,
st::multiplicable_with<acceleration>
st::multiplicable_with<acceleration>,
st::dividable_by<int>,
st::dividable_to<acceleration>
>;

using position = st::type<
Expand Down Expand Up @@ -118,6 +120,10 @@ TEST(strong_type, multiplicable)
TEST(strong_type, dividable)
{
static_assert((integer(4) / integer(2)).value() == integer(2).value());

static_assert((speed(4) / 2).value() == speed(2).value());
static_assert(std::is_same_v<decltype(speed(4) / speed(2)), acceleration>);
static_assert((speed(4) / speed(2)).value() == acceleration(2).value());
}

TEST(strong_type, modulable)
Expand Down

0 comments on commit c21788a

Please sign in to comment.