Boost logo

Boost :

Subject: Re: [boost] [mpl] Nested Scopes w/Placeholders
From: Larry Evans (cppljevans_at_[hidden])
Date: 2012-01-14 22:53:13


On 01/13/12 19:44, Kitten, Nicholas wrote:
> I've recently been doing a lot mpl::folding (in boost 1.47), and I was
> disappointed when I realized that a nested fold wouldn't work with a lambda
> expression passed to the inner call, as in this example which extracts
> elements of sequences:
>
> using namespace mpl;
> using namespace mpl::placeholders;
>
> typedef vector< vector<char>, vector<int> > vec_of_vecs;
> typedef vector< char, int > expected_result;
>
> typedef fold< vec_of_vecs,
> vector<>,
> fold< _2,
> _1,
> *push_back< _1, _2 >* // wrong - refers to
> outer arguments
> >
> >::type result;
>
> BOOST_MPL_ASSERT(( equal<result, expected_result> ));
>
>
> After looking around for a solution I came across an old
> thread<http://lists.boost.org/boost-users/2004/12/9269.php> on
> the same subject, where it was suggested using mpl::protect might work.
> Indeed, the following modification does what I want, without any changes
> to the library:
>
> typedef fold< vec_of_vecs,
> vector<>,
> fold< _2,
> _1,
> *protect< lambda< push_back< _1, _2 > >::type
>> * // correct
> >
> >::type result;
>
>
Hi Nick.

I copied your code (removing the * delimiters, which I guess you use
just to emphasize the important parts), but couldn't get either to
compile. The code I used is attached. Use the
  #define USE_LAM_PRO
to select which version to compile.

Could you modify the attached so that it does compile? I'm using
gcc-4.7.

The error messages I'm getting with gcc-4.7 include:

/home/evansl/prog_dev/boost-svn/ro/trunk/boost/mpl/fold.hpp:39:18:
required from 'struct
boost::mpl::fold<boost::mpl::vector<boost::mpl::vector<char>,
boost::mpl::vector<int> >, boost::mpl::vector<mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na>, boost::mpl::fold<mpl_::arg<2>,
mpl_::arg<1>,
boost::mpl::protect<boost::mpl::lambda<boost::mpl::push_back<mpl_::arg<1>,
mpl_::arg<2> > > > > >'
nested_placeholders.cpp:45:6: required from here
/home/evansl/prog_dev/boost-svn/ro/trunk/boost/mpl/apply_wrap.hpp:141:1:
error: no class template named 'apply' in 'struct
boost::mpl::protect<boost::mpl::lambda<boost::mpl::push_back<mpl_::arg<1>,
mpl_::arg<2> > > >'
In file included from
/home/evansl/prog_dev/boost-svn/ro/trunk/boost/preprocessor/iteration/detail/iter/forward1.hpp:52:0,
                 from
/home/evansl/prog_dev/boost-svn/ro/trunk/boost/mpl/aux_/fold_impl_body.hpp:67,
                 from
/home/evansl/prog_dev/boost-svn/ro/trunk/boost/mpl/aux_/fold_impl.hpp:40,
                 from
/home/evansl/prog_dev/boost-svn/ro/trunk/boost/mpl/fold.hpp:20,
                 from nested_placeholders.cpp:11:
/home/evansl/prog_dev/boost-svn/ro/trunk/boost/mpl/aux_/fold_impl_body.hpp:
In instantiation of 'struct boost::mpl::aux::fold_impl<1,
boost::mpl::v_iter<boost::mpl::vector<char>, 0l>,
boost::mpl::v_iter<boost::mpl::vector<char>, 1l>,
boost::mpl::vector<mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na>,
boost::mpl::protect<boost::mpl::lambda<boost::mpl::push_back<mpl_::arg<1>,
mpl_::arg<2> > > > >':
/home/evansl/prog_dev/boost-svn/ro/trunk/boost/mpl/aux_/has_type.hpp:20:1:
  recursively required from 'const bool
boost::mpl::aux::has_type<boost::mpl::fold<boost::mpl::vector<char>,
boost::mpl::vector<mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na>,
boost::mpl::protect<boost::mpl::lambda<boost::mpl::push_back<mpl_::arg<1>,
mpl_::arg<2> > > > >, mpl_::bool_<true> >::value'

-regards,
Larry




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