Boost logo

Boost :

From: Matthias Schabel (boost_at_[hidden])
Date: 2007-03-29 15:55:44


> I'm confused by this syntax. The intent here is to assign a
> quantity of
> 2 meters to an "anonymous" quantity of length, correct? If so,
> wouldn't
> the following syntax make more sense?
>
> quantity<SI::length> q = quantity<SI::meter>(2);
>
> The "2*meters" syntax just seems rather obscure. Likewise, a
> "measure",
> a quantity with a specified unit, should be constructed without the
> need
> for qualification.
>
> quantity<SI::meter> q (2);
>
> That is, the "2*meters" should not be required when the unit is
> already
> explicitly specified as part of the type.
>
> By the way, while meter is an SI unit and therefore belongs in the SI
> namespace, length is not an SI dimension hence the name "length" does
> not belong in the SI namespace. It should probably be elevated to the
> boost::units namespace. I don't believe there is such a thing as
> an "SI
> dimension"; i.e. other system of measurement (e.g. Imperial) have
> units
> of length as well.

Here's the reasoning for this syntax :

quantity<SI::length> represents a quantity of length in the SI
system, not a generic length. Because everything is specified at
compile time, units and quantities must be completely defined,
otherwise it would be impossible to achieve zero runtime overhead. In
this usage, meter is, in fact, completely equivalent to SI::length()
- it is just a static constant. Another possibility would have been
something like this : quantity<SI::meter> q(2*SI::meter());
where SI::meter is now a typedef for the full type representing an SI
unit of length (what SI::length is now). Unfortunately, as you add
more units, the extraneous parentheses become cumbersome. For
example, the definition of the ideal gas constant would go from
8.314*joules/kelvin/mole to 8.314*joules()/kelvin()/mole(). Still
legible, but definitely more difficult to follow. Furthermore, in the
current syntax, if you had a program that used a bunch of SI units
and you wanted to change it to CGS, you could just search and replace
SI->CGS and everything would work correctly. If we used the
alternative syntax, then a search and replace SI::meter-
>CGS::centimeter would do this

quantity<SI::meter> q(2*SI::meters); -> quantity<CGS::centimeter> q
(2*CGS::centimeters);

which would break the code.

Matthias


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk