|
Boost : |
From: pongba (pongba_at_[hidden])
Date: 2006-03-14 00:25:14
Okay, just image some sort of code like this:
typedef mpl::vector_c<int,1,2,3> v;
typedef mpl::accumulate<v,int_<0>,MPL_METAFUNC(_1*CONS(10)+_2)>::type::value
result_value; // =123
Imaginably, the implementation of MPL_METAFUNC would involve BOOST_TYPEOF to
deduce the type of "_1*CONS(10)+_2" so as to construct a corresponding
metafunction, that is, "mpl::plus<_2,mpl::times<_1,int_<10> > >"
(CONS here is used to work around the embarrassment that 10 becomes sort of
variable when passed directly to operator*, so we use CONS(10) which expands
into something like "int_<10>()" which entirely preserve "10" at compile-time).
Arguably this is feasible, but I lack the time to implement it fully.
(although it would be pretty simple, e.g.
#define DEFINE_BINARY_OP(op,mpl_binray_op) \
template <typename T, typename U> \
mpl_binary_op<T,U> operator op (T t, U u) \
{ \
return mpl_binary_op<T,U>(); \
} \
DEFINE_OP(+,mpl::plus)
DEFINE_OP(-,mpl::minus)
DEFINE_OP(*,mpl::times)
DEFINE_OP(/,mpl::divide)
...
Admittedly, there exists a name confliction between mpl::_1 and lambda::_1
due to the inherent difference between type and variable in the language,
so we couldn't use the same _1 and _2 in both "plus<_1,_2>" and "_1+_2".
Any good remedies for that?(of course we could always turn to "_1()+_2()"
in which _1/2 means mpl::_1/2)
My question is:
Is this valuable?(IMO,it'll make arithmetic operation in metaprogramming much
easier and clearer, just watch "_1*10+_2" v.s.
"mpl::plus<_2,mpl::times<_1,int_<10> > >" you'll see that).
And if it is, would Dave consider adding it to MPL?
Thanks in advance!
-- Regards!
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk