Boost logo

Boost Users :

Subject: Re: [Boost-users] [Units] help with type-safe unit-specific conversions
From: Matthias Schabel (boost_at_[hidden])
Date: 2009-07-23 16:34:31


> If I understand the way quantity works correctly, this will take a
> quantity on
> any length dimension and convert it to a quantity on si::length.
> What I am
> attempting to do is manage quantities in units. What I mean by
> marshalling and
> demarshalling is this:
>
> quantity<si::length> marshall(double value) {
> return value * us::foot_base_unit();
> }
>
> double demarshall(const quantity<si::length>& value) {
> return quantity</*feet*/>(value).value();
> }
>
> If possible this is the approach I'd like to take,

If you just want to do "unsafe" conversions back and forth like you've
written, why
don't you just do it directly?

quantity<si::length>::from_value(value)
quantity<us::length>(value).value()

The advantage of using wrappers like I suggested earlier is that you
then are insulated from
the units-incompatible libraries while preserving their interface and
guaranteeing (assuming
you've written the wrappers correctly) that you maintain dimensional
correctness... Maybe I'm
just not understanding your use case.

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