Boost logo

Boost :

From: David A. Greene (greened_at_[hidden])
Date: 2003-01-02 12:54:37


Aleksey Gurtovoy wrote:
> That's not to say that it should stay this way. Ideally, to support true
> mixed-type arithmetic, e.g. 'plus< rational<1,10>, int_c<5> >', the current
> MPL primitives such as 'plus', 'minus', 'multiply', etc. need a major
> re-write, to become something along these lines:
>
> template<
> typename N1
> , typename N2
> >
> struct plus
> {
> typedef typename promotion_traits<N1,N2>::type U_;
> typedef typename apply2<
> plus_traits<typename U_::tag>
> , typename construct<U_,N1>::type
> , typename construct<U_,N2>::type
> >::type type;
> };

[snip]

> Figuring out a reasonable way to specify the promotion rules is probably the
> hardest part there...

I actually did some work on this in the past but it's been sitting
on the shelf for a while and probably needs a bit of pounding into
shape. The design idea was to specify a template that acts as a
lookup table keyed on the two source types and returning the correct
result type according to a two other parameters: the operation
and a parameter I'll call the "ruleset." The ruleset can be used to
provide different promotion strategies such as ANSI/ISO C++ arithmetic
rules, "keep the most information" rules and so on.

A good example of ruleset use is the use of the "most information"
rule to implement integer divide using double types to retain precision.

Does this sound at all interesting? The implementation is complex
(didn't have MPL at the time) but can probably be cleaned up some.
It may be too flexible for your needs.

                             -Dave

-- 
"Some little people have music in them, but Fats, he was all music,
  and you know how big he was."  --  James P. Johnson

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