Boost logo

Boost Users :

Subject: Re: [Boost-users] getting started with Boost::Units
From: Matthias Schabel (boost_at_[hidden])
Date: 2009-01-09 14:21:26


> I could be misunderstanding something fundamental about
> Boost::Units. Is it possible to declare a class field or method
> parameter with a type that indicates it represents a velocity
> without specifying whether the value is in meters per second or
> miles per hour?
>
> I'd like to allow any kind of velocity to be specified and perform
> operations independent of the units being used. For example, if I
> set the velocity of some object to 60 miles per hour and then call a
> method to determine how far it will travel in 2 hours, I'd like to
> get back 120 miles. I'd also like to be able to set the velocity to
> 10 meters per second, ask the same question, and get back 72000
> meters.

You are misunderstanding something fundamental. Boost.Units was
designed to allow compile-time unit computations with no runtime
overhead. In order to achieve this, all unit information is encoded in
the unit type. The best way to write units code that is independent of
the specific units is to use templated functions; see the functions
"work" and "idealGasLaw" defined in examples/kitchen_sink.cpp. It is
also possible to do runtime conversions; see examples/
runtime_conversion_factor.cpp and examples/runtime_unit.cpp.

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