 # Boost :

From: Eric Lemings (lemings_at_[hidden])
Date: 2007-03-28 13:38:15

> -----Original Message-----
> From: boost-bounces_at_[hidden]
> [mailto:boost-bounces_at_[hidden]] On Behalf Of Steven Watanabe
> Sent: Wednesday, March 28, 2007 11:05 AM
> To: boost_at_[hidden]
> Subject: Re: [boost] [Review] Quantitative Units library
> review begins today March 26
>
...
> Allowing conversion between units having different dimensions
> and allowing fundamental units whose dimensions are not
> fundamental e.g. quarts for volume is very very difficult to
> implement. Off hand, I think that it requires defining some
> special system (probably SI) and converting through that system.

It's not that difficult but this does bring up a question that I have.
I haven't been able to answer this myself from looking at the proposal
so far.

How do you implement conversions between units that require more
complicated arithmetic expressions (e.g. units of temperature)?
Conversions for the vast majority of units are simply multiples of each
other and the conversion can be expressed at compile-time and
multiplied by a runtime quantity value:

template < typename ValueType, typename Numer, typename Denom >
ValueType convert (const ValueType& v) {
// Numer and Denom are Integral Constant Wrappers.
return v * (Numer() / Denom());
}

For units of composite dimensions, the runtime quantity value is simply
multipled by the conversion factor for each base unit.

But a few troublesome units (e.g temperature) can not be expressed so
easily and to do so at compile time requires some sort of arithmetic
lambda expression that accepts runtime values as arguments for the
placeholders. Consider the following _hypothetical_ code:

template < typename PlaceholderExpression >
struct convert {
template < typename ValueType >
ValueType apply (const ValueType& v) {
// The arity of the placeholder expression should be 1.
return apply_runtime< PlaceholderExpression > (v);
}
};

typedef typename convert <
// Hope I got this expression right. :)
divide< multiply< subtract<_1, int_<32>>, int_<10>>, int_<18>> >
> convert_Farenheit_to_Celsius;

The only alternative that I can think of is to do such conversions
completely at runtime.

Eric.