Boost logo

Boost Users :

Subject: Re: [Boost-users] Units: adding quantities
From: Matthias Schabel (boost_at_[hidden])
Date: 2009-01-09 18:23:02


> I have a method that takes a parameter of type quantity<feet>
> where feet is defined by the following typedef:
>
> typedef boost::units::us::foot_base_unit::unit_type feet;
>
> I also have this typedef:
>
> typedef bu::us::inch_base_unit::unit_type inches;
>
> Do I really need to define a typedef like this for every measurement
> type I want to use? I certainly don't want to specify all those
> namespaces every time I use one of the measurement types, but this
> approach seems a bit tedious.

If you're not using SI units, you will have to do this for units you
use. Of course, you only have to ever do this once, then put it in a
header. There are just too many (sometimes inconsistent) definitions
for non-standard units and combinations thereof to include
"everything" in the Boost.Units library...

> I'd like to do something like this:
>
> a.setWingspan(195*feet() + 8*inches());
>
> but that doesn't work because there is no match for operator+.

You need to explicitly cast one length to the type of the other :

a.setWingspan(195*feet() + quantity<feet>(8*inches()));

or

a.setWingspan(195*quantity<inches>(195*feet()) + 8*inches());

There is no way for the compiler to know which way you intended the
conversion to go. 1) it is bad design to have the compiler make
arbitrary decisions. 2) if I write 1*light_year() + 1*picometer(),
precision will be an issue. As we state in the documentation, the main
objective of the library is not to make the programmer's life easier,
per se. Rather, it is to make it easier to enforce dimensional
correctness so that numerical operations are only allowed if
everything is dimensionally correct and to do so without incurring
runtime overhead.

Matthias


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net