Boost logo

Boost :

From: Andy Little (andy_at_[hidden])
Date: 2005-04-10 04:50:43


Hi again,

With BOOST_TYPEOF in place, the mpl operators (boost::mpl::plus etc) could be
augmented by actual operators( + etc) As below:--->
Whats the point of this? Well first its fun and more legible and second brings
mpl further into runtime programming. Maybe also fits in with Alexey Gurtovoys
round lambda: http://thread.gmane.org/gmane.comp.lib.boost.devel/77267

/*
    Example
*/

#include <boost/utility/enable_if.hpp>
#include <boost/typeof/typeof.hpp>
#include <boost/mpl/plus.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/or.hpp>

// a useful addition to BOOST_TYPEOF ?

#define BOOST_AUTO_TYPEDEF(name, expr) typedef BOOST_TYPEOF(expr) name
#define BOOST_AUTO_TYPEDEF_TPL(name, expr) typedef BOOST_TYPEOF_TPL(expr) name

namespace boost{namespace mpl {

    // naive implementation of is_integral for demo only
    template <typename T>
    struct is_integral_c :false_{};
    template <int N>
    struct is_integral_c<int_<N> > : true_{};
    template <int N>
    struct is_integral_c<integral_c<int,N> > : true_{};

    template <typename Lhs, typename Rhs>
    typename boost::enable_if<
            boost::mpl::or_<
                is_integral_c<Lhs>,
                is_integral_c<Rhs>
>,
            typename plus<Lhs,Rhs>::type
>::type
    operator + (Lhs lhs, Rhs rhs)
    {
        BOOST_AUTO_TYPEDEF_TPL(result, Lhs() + Rhs());
        return result() ;
    };

}}//boost::mpl

#include <iostream>

int main()
{
    typedef boost::mpl::int_<1> one;
    typedef boost::mpl::int_<2> two;
    typedef boost::mpl::int_<3> three;
    typedef boost::mpl::int_<4> four;

    BOOST_AUTO_TYPEDEF(result , one() + two() + three() + four() ) ;

    std::cout << typeid(result).name() <<'\n';

    std::cout << one() + two() + three() + four() <<'\n';
}

reagrds
Andy Little


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