|
Boost : |
Subject: Re: [boost] [chrono] type_traits/common_type and integer/ratio
From: Howard Hinnant (howard.hinnant_at_[hidden])
Date: 2009-12-02 10:33:57
On Dec 1, 2009, at 1:15 PM, Michael Fawcett wrote:
> On Mon, Nov 30, 2009 at 7:25 AM, Vicente Botet Escriba
> <vicente.botet_at_[hidden]> wrote:
>>
>> "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>`."
>
> I've needed this feature many times. Can you explain the pros/cons of
> common_type against Boost.Typeof?
common_type is closer in nature to promote_args<class ...T> in boost/math/tools/promotion.hpp than it is to Boost.Typeof, though it is not exactly the same as promote_args either. common_type<T1, T2>::type simply represents the result of some operation on T1 and T2, and defaults to the type obtained by putting T1 and T2 into a conditional statement.
It is meant to be customizable (via specialization) if this default is not appropriate. Here is a link to the D version of the same tool:
http://www.digitalmars.com/d/2.0/phobos/std_traits.html#CommonType
-Howard
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk