Boost logo

Boost :

From: Jeremy Siek (jsiek_at_[hidden])
Date: 2001-03-29 11:46:51


I agree, we certainly do need result type traits, however I think the
requirements on the result type produced should remain what I've stated,
convertible to X and models ...

To allow for expression templates, the result type will not only
depend on the operands but also on the operator. Therefore we
need a tag for each operator.

struct add_tag { };
struct multiply_tag { };
...

and then make the operator tag a parameter to the traits class

result_traits<OpTag,A,B>::type

For builtin types, the result_traits can dispatch to the simpler promotion
traits class that does not care about the operator.

Cheers,
Jeremy

On Thu, 29 Mar 2001, Ullrich Koethe wrote:

koethe> Another very important aspect (that has no counterpart in pure math) is
koethe> type promotion. For example, Jeremy's list says:
koethe>
koethe> a + b Convertible to X and models Additive Abelian Group.
koethe>
koethe> I think we must be more specific about the type of the result. There are
koethe> clear rules for promotion of the built-in types, for example
koethe>
koethe> short + short => int
koethe> double * short => double
koethe>
koethe> These rules must be formulated in promotion_traits (invented by Todd
koethe> Veldhuizen) so that
koethe>
koethe> * the rules for new numeric types can be derived
koethe> from the rules for its constituents.
koethe> * templated algorithms can select appropriate types for
koethe> results and intermediate variables
koethe>
koethe> In my VIGRA library, I distinguish two promotion types:
koethe>
koethe> - if X is any algebraic type, then
koethe>
koethe> X op X => NumericTraits<X>::Promote
koethe> (e.g. NumericTraits<short>::Promote is int)
koethe>
koethe> - if {G, R} is an R-Module, then
koethe>
koethe> G op R => PromoteTraits<G, R>::Promote
koethe> (e.g. PromoteTraits<short, double>::Promote is double)
koethe>
koethe> In addition, there are conversion functions
koethe>
koethe> NumericTraits<T>::fromPromote() and
koethe> PromoteTraits<T,U>::firstFromPromote()
koethe>
koethe> that transform back to the original type, including necessary round-off
koethe> and clipping at the domain borders. (Actually, it's slightly different,
koethe> but you get the idea)
koethe>
koethe> Now I can do:
koethe>
koethe> template <class T, class U>
koethe> struct PromoteTraits<RGBValue<T>, RGBValue<U> >
koethe> {
koethe> typedef RGBValue<typename PromoteTraits<T, U>::Promote> Promote;
koethe> };
koethe>
koethe> and
koethe>
koethe> template <class T, class U>
koethe> typename PromoteTraits<RGBValue<T>, RGBValue<U> >::Promote
koethe> operator+(RGBValue<T> l, RGBValue<U> r)
koethe> {
koethe> typename PromoteTraits<RGBValue<T>, RGBValue<U> >::Promote res(l);
koethe> res += r;
koethe> return res;
koethe> }
koethe>
koethe> I've found this to be a very useful method which I need every day.
koethe>
koethe> Ulli
koethe> --
koethe> ________________________________________________________________
koethe> | |
koethe> | Ullrich Koethe Universität Hamburg / University of Hamburg |
koethe> | FB Informatik / Dept. of Computer Science |
koethe> | AB Kognitive Systeme / Cognitive Systems Group |
koethe> | |
koethe> | Phone: +49 (0)40 42883-2573 Vogt-Koelln-Str. 30 |
koethe> | Fax: +49 (0)40 42883-2572 D - 22527 Hamburg |
koethe> | Email: u.koethe_at_[hidden] Germany |
koethe> | koethe_at_[hidden] |
koethe> | WWW: http://kogs-www.informatik.uni-hamburg.de/~koethe/ |
koethe> |________________________________________________________________|
koethe>
koethe> To unsubscribe, send email to: <mailto:boost-unsubscribe_at_[hidden]>
koethe>
koethe>
koethe> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
koethe>
koethe>
koethe>

----------------------------------------------------------------------
 Jeremy Siek www: http://www.lsc.nd.edu/~jsiek/
 Ph.D. Candidate email: jsiek_at_[hidden]
 Univ. of Notre Dame work phone: (219) 631-3906
----------------------------------------------------------------------


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