Boost logo

Boost :

Subject: Re: [boost] [chrono] type_traits/common_type and integer/ratio
From: Vicente Botet Escriba (vicente.botet_at_[hidden])
Date: 2009-11-30 07:25:59


John Maddock wrote:
>
>> I'm looking on how to continue with the Boost.Chrono library and I would
>> like to know if
>>
>> * Boost.TypeTraits could take care of the common_type class template.
>> * Boost.Integer could take care of the ratio class template.
>
> Can you summarize what these templates are? Are they generally useful?
>
> Thanks, John.
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost
>
>

Hi,

You can see [http://www.open-std.org/jtc1/sc22/wg21 [*N2661 - A Foundation
to Sleep On]] which is very informative and provides motivation for key
design decisions for `common_type`, ratio and chrono.

I really think they are very useful and the standard group also, as both are
in N3000.

Here there are some extracts

"In a nutshell, `common_type` is a trait that takes 1 or more types, and
returns a type which all of the types will convert to. The default
definition demands this conversion be implicit. However the trait can be
specialized for user-defined types which want to limit their inter-type
conversions to explicit, and yet still want to interoperate with the
`common_type` facility.

Example:

    template <class T, class U>
    typename common_type<complex<T>, complex<U> >::type
    operator+(complex<T>, complex<U>);

In the above example, "mixed-mode" complex arithmetic is allowed. The return
type is described by `common_type`. For example the resulting type of adding
a `complex<int>` and `complex<double>` might be a `complex<double>`."

and

"`ratio` is a general purpose utility inspired by Walter Brown allowing one
to easily and safely compute rational values at compile time. The `ratio`
class catches all errors (such as divide by zero and overflow) at compile
time. It is used in the `duration` and `time_point` classes to efficiently
create units of time. It can also be used in other "quantity" libraries
(both std-defined and user-defined), or anywhere there is a rational
constant which is known at compile time. The use of this utility can greatly
reduce the chances of run time overflow because the `ratio` (and any ratios
resulting from `ratio` arithmetic) are always reduced to lowest terms.

`ratio` is a template taking two intmax_ts, with the second defaulted to 1.
It only has two public members, both of which are static const intmax_t. One
is the numerator of the `ratio` and the other is the denominator. The
`ratio` is always normalized such that it is expressed in lowest terms, and
the denominator is always positive. When the numerator is 0, the denominator
is always 1.

[*Example:]

    typedef ratio<5, 3> five_thirds; // five_thirds::num == 5,
five_thirds::den == 3
    typedef ratio<25, 15> also_five_thirds; // also_five_thirds::num == 5,
also_five_thirds::den == 3
    typedef ratio_divide<five_thirds, also_five_thirds>::type one; //
one::num == 1, one::den == 1

"

You can find prototypes implementation on the sandbox
/chrono/boost/type_trais/common_type.hpp and /chrono/boost/ratio.hpp.

Thanks for your interest,
Vicente

-- 
View this message in context: http://old.nabble.com/-chrono--type_traits-common_type-and-integer-ratio-tp26555530p26573874.html
Sent from the Boost - Dev mailing list archive at Nabble.com.

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