Blair Davidson
Blair Davidson

Reputation: 951

boost mpl integral type accumulate

How do I add the numbers?

typedef boost::mpl::vector<
    boost::mpl::int_<1>, boost::mpl::int_<2>,
    boost::mpl::int_<3>, boost::mpl::int_<4>,
    boost::mpl::int_<5>, boost::mpl::int_<6> > ints;
typedef boost::mpl::accumulate<ints, boost::mpl::int_<0>, ????? >::type sum;

Upvotes: 2

Views: 696

Answers (1)

Anonymous
Anonymous

Reputation: 18631

EDIT: I was wrong, you can use mpl::plus directly, using the placeholder expressions. This simplifies the whole notation:

typedef mpl::accumulate<ints, mpl::int_<0>, mpl::plus<mpl::_1, mpl::_2>  >::type sum;

Of course it is also possible to obtain the same effect using a metafunction class (which for adding is an overkill, but for something more complex might be reasonable):

struct plus_mpl
{
    template <class T1, class T2>
    struct apply
    {
       typedef typename mpl::plus<T1,T2>::type type;
    };
};

typedef mpl::accumulate<ints, mpl::int_<0>, plus_mpl >::type sum;

Upvotes: 3

Related Questions