How can I implement nested boost::mpl::fold ?
namespace mpl=boost::mpl;
typedef mpl::vector_c<int,1,1,1> vec1;
typedef mpl::vector_c<int,2,2,2> vec2;
typedef mpl::vector_c<int,3,3,3> vec3;
typedef mpl::vector<vec1,vec2,vec3> vvec;
typedef typename mpl::lambda
<mpl::fold
<mpl::_1
,mpl::int_<0>
,typename mpl::lambda<mpl::plus<mpl::_1,mpl::_2>>::type
>
>::type lam;
typedef typename mpl::fold
<vvec
,mpl::int_<0>
,mpl::plus<mpl::_1,typename lam::template apply<mpl::_2>::type>
>::type result;
BOOST_MPL_ASSERT((mpl::equal_to<result,mpl::int_<18>>));
I want result to be 18, but above equals to 0.
Nested binds always invite collisions between placeholders: the lambda uses the same placeholders as the outer fold does, and the substitution will do the wrong thing.
Boost Lambda, Boost Bind and yes, even Boost Mpl implement the
protect
/unprotect
combo so you can fix this:Live On Coliru