nholthaus / units

a compile-time, header-only, dimensional analysis and unit conversion library built on c++14 with no dependencies.
http://nholthaus.github.io/units/
MIT License
947 stars 135 forks source link

explicit T& operator() #186

Open Guillaume227 opened 5 years ago

Guillaume227 commented 5 years ago

Currently there is an explicit T operator()() const to retrieve the underlying value. I would like to get access to the underlying value as a non-const reference to modify it : explicit T& operator()()

Motivation: I would like to reuse some numerical utils methods that take doubles e.g.

bool my_method(double& a, double b){
   // modifies a
}

I could template my_method on a unit type but I am concerned with two things: 1) it is intrusive - I would like to avoid modifying the existing code if possible 2) (more importantly) it will lead to different template instantiations for each unit type I am using and I must keep binary size to a minimum (embedded context). So I thought I could do:

template<typename U>
bool my_method(U& a, U b){
    return my_method(static_cast<double&>(a), static_cast<double>(b));
}

which the compiler should be able to compile away entirely, giving me zero-compiled-size-and-runtime-cost

Potential issue: that would work only for linear_scale, not for DB scale (but I am only using linear). I have a simple change for that, would you consider a PR for that feature?