Skip to content

Commit

Permalink
Fix ambiguity when using operator* with the multiplicable trait
Browse files Browse the repository at this point in the history
  • Loading branch information
doom committed Jul 30, 2019
1 parent bacdc52 commit 394d8fa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions include/st/traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ namespace st
template <typename T, typename OtherOperandT = T, typename ReturnT = T>
struct multiplicable
{
constexpr ReturnT operator*(const OtherOperandT &other) const noexcept
friend constexpr ReturnT operator*(const T &lhs, const OtherOperandT &other) noexcept
{
return ReturnT(static_cast<const T *>(this)->value() * unwrap(other));
return ReturnT(lhs.value() * unwrap(other));
}

template <typename _T = T, typename _Other = OtherOperandT,
Expand Down
21 changes: 21 additions & 0 deletions tests/strong_type-tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,24 @@ TEST(strong_type, bitwise_manipulable)
static_assert(~(~bitwise_number(1)) == bitwise_number(1));
static_assert((bitwise_number(45) ^ bitwise_number(212) ^ bitwise_number(45)) == bitwise_number(212));
}

TEST(strong_type, price_spread)
{
using spread = st::type<
double,
struct spread_tag,
st::arithmetic
>;

using price = st::type<
double,
struct price_tag,
st::arithmetic,
st::multiplicable_with<spread>
>;

constexpr price mid(120);
constexpr spread sp(0.5);
constexpr price pe = mid * (spread{1.0} - sp);
static_assert(pe == price{60.0});
}

0 comments on commit 394d8fa

Please sign in to comment.