Comments (5)
A few things:
- I assume you're working from branch
2.2.x
? The master branch hasn't had anyconstexpr
support pulled in yet. - Assuming that, I'm pretty sure it's not possible to do
constexpr
class assignment in c++11 becauseconstexpr
impliesconst
. That requirement was relaxed in c++14. - c++11 also has a requirement that
constexpr
functions are written in a single return statement, which this library generally does not conform to, because it was also relaxed in c++14. - Currently, assignment operators in
units
aren't markedconstexpr
, and my hazy recollection is because if they were you wouldn't be able to compileunits
with c++11 at all because of the above-mentioned property ofconstexpr
. That said, units is a c++14 library so I will reserve the right to change that on a whim in future versions.
Overall, my approach to compiling units with c++11 is this: if it works for your application, great :) If not, you may need a c++14 compiler. C++11 as a standard isn't really sufficient for a proper library implementation in my opinion.
workaround
I'm also pretty sure (that on branch 2.2.x
) you could refactor that statement and have it compile as expected (using c++11). Try:
// not sure area is the right name for a unit of length
constexpr units::length::meter_t area(5);
Also, I think even with gcc 4.9.8 you can use the -std=c++1y
to compile with c++14, but it looks like the relaxed constexpr
requirements didn't make it in until gcc 5. Even so, that would be my recommendation when compiling with units, if possible.
workaround 2
Units has a type called unit_value_t
which was intended for compile-time constant unit values for compilers without constexpr
support (primarily visual studio 2013). It's basically a std::ratio
that has a dimension associated with it. Check out the Pythagorean Theorem unit test and its Right Triangle Class to see how it works.
In many ways unit_value_t
is actually better than constexpr
, because it supports compile-time pow
and sqrt
, which <cmath>
doesn't have. The only caveat is that since it's a ratio your values need to be rational numbers (or rational approximations). Usage would look something like:
using a = unit_value_t<meters, 5>; // access this in a calculation with `a::value()` static function.
from units.
Glad you found one that works for you! I'm very familiar with PhysUnits-CT-Cpp11 and it's a great library, and Martin is well known for creating modern libraries with a wide range of compiler support.
It definitely looks like the assignment can be done in c++11 from your example so I'll still probably incorporate that into my next library version.
Aside from the occasional issue like this one, I think the choice between those libraries mostly comes down to your taste for the boost-like quantity
syntax or the unit-centric syntax.
I implemented my original CT sqrt
using a recursive Newton-Raphson method, but did not like the performance I was getting (lots of arithmetic overflow in the ratios), and ended up switching to a continued fraction method with a CT binary search to determine the initial conditions. I also think it's best to keep all conversions as rational values until the very end, since all conversion factors (minus PI) are rational anyway, and it minimizes floating-point rounding errors.
from units.
This is confirmed working in the v2.2.x branch and the soon-to-be v2.2.0 release. I think your testing must have been done on a copy of the master branch units.h
which didn't include constexpr
support prior to v2.2.0.
from units.
@nholthaus thank you for the feedback!
We tried out several (zero-overhead) unit systems yesterday, looking for one to perform conversions at CT, running with gcc 4.9 and C++11 for CUDA 7.5 compatibility, and actually found that PhysUnits-CT-Cpp11 allows what I was looking for:
#include "phys/units/quantity.hpp"
#include "phys/units/physical_constants.hpp"
using namespace phys::units;
using namespace phys::units::literals;
constexpr quantity<length_d, float> dx = 0.6e-6_m;
// we are in a device kernel now, and conversion can be performed as a C++11 constexpr:
__device__ void f()
{
constexpr float dx_scaled = dx.to( unit_length );
constexpr float Lx = 100. * dx;
// ... do work
}
The unit_value_t
examples you linked are pretty impressive for pre-constexpr, cool!
In case that is interesting for you, my colleague @psychocoderHPC found a stackoverflow answer yesterday with C++11 constexpr sqrt
for floating point vars.
from units.
FYI: There is no assigment operator involved in the original example. Just the copy constructor.
from units.
Related Issues (20)
- Request for branch and pull request permissions HOT 5
- percent_t FROM double and TO double are different HOT 9
- 2.3.3 does not compile
- Add a way to specify units when "downcasting" to numeric type HOT 2
- Support the MSFS SDK HOT 2
- Empty base class optimization for MSVC
- Does not compile with GCC 12 HOT 1
- raw() and value() is error prone HOT 4
- [Bug] i386 (32-bit) fails to compile
- Shouldn't the naming of units::torque::foot_pound be changed
- v2 -> v3 porting HOT 2
- Need help implementing resistance as a new custom unit. HOT 2
- unit conversion emits a surprisingly high amount of instructions
- Compilation under MINGW
- Math functions not compatible with percent HOT 1
- Status of the project HOT 14
- Units are not installed as a system library HOT 1
- How to print units with C++20 std::format()? HOT 1
- Missing decibel litteral ?
- How to get the unit value with correct scaling ? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from units.