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

Error with overloaded operators in the Quantity class #15

Open
markusrunkel opened this issue Mar 15, 2021 · 4 comments
Open

Error with overloaded operators in the Quantity class #15

markusrunkel opened this issue Mar 15, 2021 · 4 comments

Comments

@markusrunkel
Copy link

When using a specification of the Quantity class, the wrong operator is used for multiplication and division of two such specifications.
Instead of the operator which calculates two quantities, the operator which uses only one quantity and one template parameter is used.

Instead of

template <typename DX, typename DY, typename X, typename Y>
friend constexpr detail::Product<DX, DY, X, Y>
operator*( quantity<DX, X> const & lhs, quantity< DY, Y > const & rhs );

template <typename DX, typename DY, typename X, typename Y>
friend constexpr detail::Quotient<DX, DY, X, Y>
operator/( quantity<DX, X> const & x, quantity< DY, Y > const & y );

the following operators are used, e.g.:

template <typename D, typename X, typename Y>
friend constexpr quantity<D, detail::PromoteMul<X,Y>>
operator*( quantity<D, X> const & x, const Y & y );

template <typename D, typename X, typename Y>
friend constexpr quantity<D, detail::PromoteMul<X,Y>>
operator/( quantity<D, X> const & x, const Y & y );

Proposed sloution

In the overloaded binary and assignment operators, which are handling a typename and a quantity, a fourth template parameter was added.
The fourth template parameter enables the function only if the typename is convertible to the underlying type of the quantity.

For example:

template< typename D, typename X, typename Y, typename = std::enable_if_t<std::is_convertible<Y,X>::value>>
constexpr quantity<D, X> &
operator/=( quantity<D, X> & x, const Y & y )
@martinmoene
Copy link
Owner

@markusrunkel thanks for your report. I hope to look at it further in a few days.

@martinmoene
Copy link
Owner

@markusrunkel can you give a minimal compilable example that shows what you describe?

@markusrunkel
Copy link
Author

@martinmoene thank you for answer.

#include <phys/units/quantity.hpp>

template<typename T>
struct Specification: public T 
{
  Specification(const T& aValue): T(aValue) {}; 
};

using Length = Specification<phys::units::quantity<phys::units::length_d>>;

int main () {
  auto rectangleLength = Length{2 * phys::units::meter};
  auto rectangleWidth = Length{3 * phys::units::meter};

  auto area = rectangleLength * rectangleWidth;
}

The problem I have is in the multiplication in the main function.

@martinmoene
Copy link
Owner

@markusrunkel ok, thanks.

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

No branches or pull requests

2 participants