[KUnitConversion] Question about overloading operators

Milian Wolff mail at milianw.de
Mon Nov 21 12:50:39 UTC 2016


On Monday, November 21, 2016 8:27:11 AM CET Ander Pijoan wrote:
> Hi Aleix,
> 
> I think at least the most desirable basic feature would be to have the
> math operators overloaded to be able to operate with Values. Something like
> 
> 
> Value operator +(const Value &other)
> {
>       Q_ASSERT( unit() == other.unit() );
>       return Value(  number() + other.number() , unit()  );
> }
> 
> At least to be able to operate with same Unit Values.
> I have an application where I was calculating several routes. This week I
> started using KUnitConversion
> Values for the length, max speed and price of each of each road segment, in
> order to explicitly
> have the units "stored" within the value. But suddenly found I can't even
> sum two lengths because the
> + operator is not overloaded.
> 
> If further operations involving different units conversions want to be
> supported, probably some sort of
> equivalence structures would need to be created, but I havent think much of
> it. I was just asking about single
> Unit operations.

Improving KDE software is of course always good, but maybe you want to use a 
different library for something like that. You most probably want to have a 
compile-time checked unit system, which is possible in C++. There is e.g.

https://github.com/nholthaus/units

and also

http://www.boost.org/doc/libs/1_61_0/doc/html/boost_units.html

Both of which provide more safety.
-- 
Milian Wolff
mail at milianw.de
http://milianw.de


More information about the Kde-frameworks-devel mailing list