Boost logo

Boost :

From: Steven Watanabe (steven_at_[hidden])
Date: 2006-08-07 12:47:43


AMDG

Alexander,

    This method seems to cover everything I can think of that is
actually possible:

struct plus : function<plus, anyT (anyT const&, anyT const&)> {
    template<class R, class T0, class T1>
    R entry(const T0& t0, const T1& t1) {
       return(t0 + t1);
    }
}

template<>
struct overload<plus, int> {
    typedef typename transform<
       integral_types,
       binary_function<promotion_traits<int, _1>, int, _1>
>::type type;
}

//other integral types are similar

template<>
struct overload<plus, string> {
    typedef vector<
       string(const string&, const string&),
       string(const string&, const char*),
       string(const char*, const string&)
> type;
};

This allows new types to be added without changing plus
at all. It should be fast since in most cases there will not
be a lot of possible overloads after the initial lookup.

There would of course be a catch-all when none
of the overloads matches. By default it should probably
be something like:

    void no_match(const any<L>& any0, const any<L>& any1) {
       throw(undefined_function(typeid(plus), any0.type(), any1.type()));
    }

I haven't thought of any real disadvantages to this approach.
But, as I haven't tried to implement it yet, I may have missed something.

In Christ,
Steven Watanabe


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