I'm trying to create a metafunction which takes in a boost::mpl::vector containing spirit parser types, and yields a vector of the original vector's length plus one with each element representing the parser type made of the recursive boost::spirit::alternative of all of the original parser types from the corresponding element in the original vector and an epsilon_parser at the end. in other words, the metafunction should be able to take in: (using boost::mpl and boost::spirit) vector< chlit<>, strlit<>, range<> > and yield a result: vector< alternative< chlit<>, alternative< strlit<>, alternative< range<>, epsilon_parser > > >, alternative< strlit<>, alternative< range<>, epsilon_parser >
, alternative< range<>, epsilon_parser >, epsilon_parser >
where empty is my own parser type, similar to epsilon_parser I'm still new to metaprogramming and the mpl lambda facilities and I'm having some trouble creating a metafunction which produces my desired type. This is what I've come up with, though it doesn't work - I'm assuming because of my second use of mpl::_1 in the nested template parameter of mpl::front in mpl::push_front (which I'd like to have represent the current state of the mpl::fold). template< typename vector_t > struct get_alternatives { typename mpl::fold_backward< vector_t, mpl::vector< epsilon_parser >, typename mpl::push_front< mpl::_1, spirit::alternative< mpl::_2, typename mpl::front< mpl::_1 >::type > >::type >::type type; }; Any help is greatly appreciated. Thanks in advance.
Matt Calabrese wrote:
<snip> I'm still new to metaprogramming and the mpl lambda facilities and I'm having some trouble creating a metafunction which produces my desired type. This is what I've come up with, though it doesn't work - I'm assuming because of my second use of mpl::_1 in the nested template parameter of mpl::front in mpl::push_front (which I'd like to have represent the current state of the mpl::fold).
template< typename vector_t > struct get_alternatives { typename mpl::fold_backward< vector_t, mpl::vector< epsilon_parser >, typename mpl::push_front< mpl::_1,
spirit::alternative< mpl::_2,
typename mpl::front< mpl::_1 >::type > >::type >::type type; };
The problem is that you are evaluating the nested metafunction calls. What you want here is lazy evaluation: template< typename vector_t > struct get_alternatives { typedef typename mpl::fold_backward< vector_t , mpl::vector< epsilon_parser > , mpl::push_front< mpl::_1 , spirit::alternative< mpl::_2 , mpl::frontmpl::_1 > > >::type type; }; typename mpl::frontmpl::_1::type will evaluate mpl::front<> on a placeholder argument, which doesn't make any sense. HTH, -- Daniel Wallin
participants (2)
-
Daniel Wallin
-
Matt Calabrese