Boost logo

Boost Users :

Subject: Re: [Boost-users] [Units] help with type-safe unit-specific conversions
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2009-07-22 16:58:30


AMDG

Justin Leonard wrote:
> I have been playing with Boost.Units as part of a work project recently, and
> I am not sure what the best approach to "demarshalling" values in a specific
> unit. For example, I would like to do something like:
>
> using namespace boost::units;
>
> struct A {
> enum { feet, kilometers };
>
> quantity<si::length> some_length;
>
> double foo(int type) {
> switch(type) {
> case feet:
> return quantity<feet>(some_length).value();
> case kilometers:
> return quantity<kilometers>(some_length).value();
> }
> }
> };
>
> This approach is necessary for passing values to external libraries which do
> not (and probably will never) use Boost.Units. Is conversion_factor the
> best approach, or is there a way to achieve this implicitly using the type
> system?
>

If you substitute the correct unit types in place of the
enum values in quantity<xxx>(some_value).value(),
then the code above should work fine.

I'm not quite sure what you mean by achieving this
implicitly using the type system. You have define
the mapping from enum values to unit types somehow.
There is no way for it to be deduced automatically.

In Christ,
Steven Watanabe


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