GithubHelp home page GithubHelp logo

units-cxx14's People

Contributors

fay59 avatar jplatte avatar trass3r avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

trass3r graydon

units-cxx14's Issues

Unit definitions can be changed, fix creates a new problem

Right now example.cpp doesn't define units as const or constexpr. This means that they themselves can't be defined in header files without some ugly tricks, and physical constants can't be either.

There is no direct technical problem with declaring them constexpr, which solves the above problem, but there is an indirect problem in it, because now to declare mutable unit variables, you'd have to write std::remove_const<decltype(_x)>::type instead of just decltype(x).

I have two ideas on how to fix this:

  1. Create macros, something like CONST_UNIT(u) and VAR_UNIT(u), that hide decltype and std::remove_const
  2. Don't fix it in the header and instead change the example to create typedefs like typedef std::remove_const<decltype(_V)>::type voltage, so the user then has voltage and const voltage as types.

I'm not sure about 2., it might give the illusion that you can manage different physical dimensions with the same unit with units-cxx14 (distinguish between potential and voltage [both V], or a dimensionless value and an angle [both unitless]). But I can't say I like macros either. What do you think?

degrees/radians

Currently radians are commented out: // skipping radian: rad = m / m
and degrees result in a primitive type losing type safety.

void foo(decltype(deg) a)
{}
void unitstest()
{
    foo(5 * rad);
}

Definition of Hz as 1/s doesn't work

I added it to example.cpp as a very simple testcase.

Code:

auto _Hz = 1 / _s;

g++ 4.9.2 error message:

In file included from example.cpp:26:0:
units.hpp: In instantiation of ‘constexpr typename std::enable_if<std::is_arithmetic<_Tp>::value, unitscxx::quantity<NT, N, D> >::type unitscxx::operator/(MulType, unitscxx::quantity<NT, N, D>) [with MulType = int; NT = double; N = detail::sequence<units, (units)1>; D = detail::sequence<units>; typename std::enable_if<std::is_arithmetic<_Tp>::value, unitscxx::quantity<NT, N, D> >::type = unitscxx::quantity<double, detail::sequence<units, (units)1>, detail::sequence<units> >]’:
example.cpp:43:16:   required from here
units.hpp:367:36: error: could not convert ‘unitscxx::quantity<double, detail::sequence<units>, detail::sequence<units> >((double)left).unitscxx::quantity<NumericType, Numerator, Denominator>::operator/<double, detail::sequence<units, (units)1>, detail::sequence<units> >(right)’ from ‘unitscxx::quantity<double, detail::sequence<units>, detail::sequence<units, (units)1> >’ to ‘std::enable_if<true, unitscxx::quantity<double, detail::sequence<units, (units)1>, detail::sequence<units> > >::type {aka unitscxx::quantity<double, detail::sequence<units, (units)1>, detail::sequence<units> >}’
   return unitless_quantity(left) / right;
                                    ^
units.hpp:368:2: error: body of constexpr function ‘constexpr typename std::enable_if<std::is_arithmetic<_Tp>::value, unitscxx::quantity<NT, N, D> >::type unitscxx::operator/(MulType, unitscxx::quantity<NT, N, D>) [with MulType = int; NT = double; N = detail::sequence<units, (units)1>; D = detail::sequence<units>; typename std::enable_if<std::is_arithmetic<_Tp>::value, unitscxx::quantity<NT, N, D> >::type = unitscxx::quantity<double, detail::sequence<units, (units)1>, detail::sequence<units> >]’ not a return-statement
  }
  ^

clang++ 3.5.0 error message:

In file included from example.cpp:26:
./units.hpp:367:10: error: no viable conversion from 'quantity<[...], sequence<[...], (no argument)>, sequence<[...], 1>>' to 'quantity<[...],
      sequence<[...], 1>, sequence<[...], (no argument)>>'
                return unitless_quantity(left) / right;
                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
example.cpp:43:14: note: in instantiation of function template specialization 'unitscxx::operator/<int, double, detail::sequence<units, 1>,
      detail::sequence<units> >' requested here
auto _Hz = 1 / _s;
             ^
./units.hpp:228:13: note: candidate constructor not viable: no known conversion from 'unitscxx::quantity<double, detail::sequence<units>,
      detail::sequence<units, 1> >' to 'const unitscxx::quantity<double, detail::sequence<units, 1>, detail::sequence<units> > &' for 1st argument
                constexpr quantity(const quantity&) = default;
                          ^
./units.hpp:229:13: note: candidate constructor not viable: no known conversion from 'unitscxx::quantity<double, detail::sequence<units>,
      detail::sequence<units, 1> >' to 'unitscxx::quantity<double, detail::sequence<units, 1>, detail::sequence<units> > &&' for 1st argument
                constexpr quantity(quantity&&) = default;
                          ^
1 error generated.

Also, for everything else to work with g++ 4.9, I first had to make a small change to the header file (see the second revision of my gist). Anyway, I don't really understand what this error message says.

Should we allow operators to work with quantities of different NumericType's?

This is already working for the overloads of * and / for two quantities, but + and - for two quantities, as well as pretty much every operator with a quantity on one side and a primitive numeric type on the other requires equal numeric types.

If you don't see a problem with generalizing these functions to everything where std::is_arithmetic<NT>::value is true, I could make a PR.

units.hpp:38:20: error: ‘size_t’ does not name a type

Pretty self-explanatory, simply a missing include of <cstddef>.

Also, two three other small things:

  • Is there a reason you don't have include guards? I guess you don't strictly need them, as your code is 100% templates, but it still seems to be reasonable to have them.
  • Is there a reason the quantity constructors and const member functions aren't constexpr? It's the same thing for the non-member operator* and operator/.
  • Why are quantity& operator+=(quantity that) and quantity& operator-=(quantity that) const?

Here's an updated version (I don't like PRs, just create your own commits, you can select individual changes using -p / --patch with git add): https://gist.github.com/jplatte/96cf29cba575d6e8de53

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.