I found this counterintuitive result of applying the unary minus operator to a unit
"- si::meter" is the same as "si::meter" (or any other unit).
or worst
std::cout << - si::meter*1. << std::endl; // prints 1*m
i.e. the minus is explicitly ignored. The root of the problem is this curious line in boost/units/unit.hpp
I agree this is counterintuitive - the intent was that the unary operators should not affect units.
a more intuitive result (but not completely elegant) could be attained by defining instead:
template
quantity > operator-(unit const& u){ return -1.*u; }
This would be inconsistent with library architecture...
Note aside: Why would I need "minus units" in the first place? The original code that produced the confusion was
- kB*T
which appears commonly in physics. kB is the "atomic" unit of entropy/ specific heat (atomic::heat_capacity) and T is a temperature quantity. This was parsed as
(-kB)*T and then as kB*T
i.e. the minus sign was ignored all together.
In my opinion unary operator- should not be defined for units in the first place. The current definition only makes things worst, by creating a counter intuitive result AND making the fix conflict with the definition.
I think you're right that it is more correct for those operators to be undefined in this case.
Try the following and see if it works for you : in unit.hpp make the following change to lines 102-116 :
/// unit unary plus typeof helper
/// INTERNAL ONLY
//template