Boost logo

Boost :

Subject: [boost] arithmetic meta functions and traits
From: Zachary Turner (divisortheory_at_[hidden])
Date: 2009-12-06 03:18:09


Is there any use for something like this? Following compiles on MSVC 9.0.
Despite the simplicity of the final template, it was *NOT* easy to get this
working given that there's about 10 ways you should be able to do this
according to the standard, but poor SFINAE support makes it next to
impossible to get your compiler to accept anything. Since it doesn't do
anything highly fancy, I would wager it works on GCC and other compilers as
well, although it needs to be tested.

struct no_type { };

#define MAKE_BINOP_METAFUNCTIONS(op, binop, traits) \
template<typename T, typename U> no_type operator op (T, U); \
template<typename T, typename U> \
struct traits \
{ \
    typedef BOOST_TYPEOF(T() op U()) type; \
}; \
template<typename T, typename U> \
struct binop \
{ \
private: \
    typedef typename traits<T,U>::type result_type; \
public: \
    static const bool value = !boost::is_same<result_type,no_type>::value; \
};

MAKE_BINOP_METAFUNCTIONS(+, is_addable, addition_traits);
MAKE_BINOP_METAFUNCTIONS(-, is_subtractable, subtraction_traits);
MAKE_BINOP_METAFUNCTIONS(/, is_dividable, division_traits);
MAKE_BINOP_METAFUNCTIONS(*, is_multipliable, multiplication_traits);

Obviously you can define other operators similarly, as well as unary
operators.

Zach


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