fay59 / units-cxx14 Goto Github PK
View Code? Open in Web Editor NEWA header library for calculations with units
License: MIT License
A header library for calculations with units
License: MIT License
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:
CONST_UNIT(u)
and VAR_UNIT(u)
, that hide decltype
and std::remove_const
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?
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);
}
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.
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.
Pretty self-explanatory, simply a missing include of <cstddef>
.
Also, two three other small things:
operator*
and operator/
.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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.