Boost logo

Boost :

From: Tobias Schwinger (tschwinger_at_[hidden])
Date: 2006-05-17 17:48:51


Hi Dan & Joel,

>>Tobias Schwinger wrote:
>>> (It would also be cool if one could just typedef the lambda expression to nested
>>> result or inherit from a wrapper. I don't know whether it's possible and a good
>>> idea - this part is just loud thinking)...
>
> // rtc stands for runtime code
>
> struct a_func
> : result< munch< _1, tweak<_2> > >
> {
> template<typename A, typename B>
> typename result<A,B>::type operator()(A const & a, B const & b) const
> {
> return rtc::munch(a,rtc::tweak(b));
> }
> };
>

Here's an implementation.

I changed the name from 'result' to 'lambda' (because nested classes shouldn't
have the same name as the enclosing ones).

  FILES:
  ======

    o lambda.hpp MPL-lambda adapter for result computation
    o lambda_test.cpp test/example for MPL-lambda adapter

  TESTED WITH:
  ============

    o GCC 3.4.2
    o MSVC 8.0
    o MSVC 7.1

It works really well -- the technique also reduces ambiguity problems
caused by ADL with mutliple namespaces!

Hope you like it as much as I do...

Regards,

Tobias


// (C) Copyright Tobias Schwinger
//
// Use modification and distribution are subject to the boost Software License,
// Version 1.0. (See http://www.boost.org/LICENSE_1_0.txt).

//------------------------------------------------------------------------------

#include <boost/type.hpp>
#include <boost/fusion/algorithm/transformation/transform.hpp>
#include <boost/fusion/sequence/container/vector.hpp>
#include <boost/fusion/sequence/generation/make_vector.hpp>

#include <boost/fusion/support/lambda.hpp>

// namespace setup

namespace rmf // result metafunctions
{
    using namespace boost::fusion::result_of;
    using namespace boost::mpl::placeholders;
    using boost::add_const;
}

namespace rtc // runtime code
{
    using namespace boost::fusion;
}

// ---- transform function

namespace rmf
{
    typedef lambda< boost::type<_> > a_transform_op;
}

namespace rtc
{
    using namespace boost::mpl::placeholders;

    struct a_transform_op
      : rmf::a_transform_op
    {
        template<typename T>
        typename result<T>::type operator()(T const &) const
        {
            return boost::type<T>();
        }
    };
}

// ---- user defined algorithm built on top of fusion::transform

namespace rmf
{
    typedef lambda< transform<add_const<_>, rtc::a_transform_op> > a_transform;
}

namespace rtc
{
    template<typename Seq>
    typename rmf::a_transform::template result<Seq>::type
    inline a_transform(Seq const & seq)
    {
      return rtc::transform(seq, a_transform_op());
      // qualification pleases gcc
    }

    void test()
    {
      a_transform(make_vector("hi","there"));
    }
}


// (C) Copyright Tobias Schwinger
//
// Use modification and distribution are subject to the boost Software License,
// Version 1.0. (See http://www.boost.org/LICENSE_1_0.txt).

//------------------------------------------------------------------------------

#ifndef BOOST_FUSION_LAMBDA_HPP_INCLUDED
#define BOOST_FUSION_LABBDA_HPP_INCLUDED

#include <boost/mpl/lambda.hpp>

#include <boost/preprocessor/repetition/enum_params.hpp>
#include <boost/preprocessor/repetition/enum_binary_params.hpp>
#include <boost/preprocessor/facilities/intercept.hpp>

namespace boost { namespace fusion { namespace result_of {

    template<typename PlaceholderExpression>
    struct lambda
    {
        template
        < BOOST_PP_ENUM_BINARY_PARAMS(BOOST_MPL_LIMIT_METAFUNCTION_ARITY,
              typename T, = mpl::na BOOST_PP_INTERCEPT)
>
        struct result
          : mpl::lambda<PlaceholderExpression>::type::template apply<
                BOOST_PP_ENUM_PARAMS(BOOST_MPL_LIMIT_METAFUNCTION_ARITY, T) >
        { };
    };

}}}

#endif


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