Boost logo

Boost :

From: Andy Little (andy_at_[hidden])
Date: 2004-02-03 15:06:02


"Andy Little" <andy_at_[hidden]> wrote in message
news:bvnupa$h14$1_at_sea.gmane.org...
>
> Though perhaps this might work:
>
> result_of<myClass, '+' , myClass>::type t = x+y;
> or
> result_of< operator_<'+'>,myClass,myClass >::type t = x + y;
>
> maybe I'll experiment with that.

Hows about this:

regards
Andy Little
----------------------------

// useage
// binary_result<A,op,B>::type

// type is the type of the result of A op B
// op represented by operators in <functional>

//Alexey Gurtovois promotion header
// slightly hacked
#include "types_promotion_traits.hpp"
#include <functional>
#include <iostream>

template <
    typename A,
     /// just want to reuse the name from <functional>
    template<typename> class Binary_operator,
    typename B
> struct binary_result{
    // usually does the right thing
    typedef typename boost::types_promotion_traits<
        A,
        B
>::type type;
};

// specialisations for comp ops
template<
    typename A,
    typename B
>
    struct binary_result<
        A,std::less,B
>{
    typedef bool type;
};
// udt ops
struct Dummy1{};
struct Dummy2{};
struct Dummy3{};

template<>
struct binary_result<
    Dummy1,std::multiplies,Dummy2
>{
    typedef Dummy3 type;
};

int main() {
    std::cout << typeid(binary_result<
        char,std::plus,char
>::type).name() <<'\n';
    std::cout << typeid(binary_result<
        float,std::multiplies,int
>::type).name() <<'\n';
    std::cout << typeid(binary_result<
        char,std::less,double
>::type).name() <<'\n';
    std::cout << typeid(binary_result<
        Dummy1,std::multiplies,Dummy2
>::type).name() <<'\n';
}
/*
output VC7.1:
int
float
bool
struct Dummy3
*/


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