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:


You can see [ [*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.


    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>`."


"`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.


    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,

View this message in context:
Sent from the Boost - Dev mailing list archive at

Boost list run by bdawes at, gregod at, cpdaniel at, john at