Preprocessor lib question -- how to fold a macro that produces comma in its output across a sequence?

Hi, I am trying to use the preprocessor lib to generate something like Cons<a, Cons<b, Cons<c, Cons<d, Nil> > > > from (a) (b) (c) (d) I tried something like #define START(x) Cons<x,Nil> #define ADD(s,state,x) Cons<x,state > #define GENERATE(x) BOOST_PP_SEQ_FOLD_RIGHT(ADD,START(BOOST_PP_SEQ_HEAD(BOOST_PP_SEQ_REVERSE(x))),BOOST_PP_SEQ_POP_BACK(x)) but immediately got into trouble with the commas. The best I can do is to add parentheses around the content of START and ADD, but that generated (Cons<a, (Cons<b, (Cons<c, (Cons<d, Nil>) >) >) >) which is not valid. Is there any way I can achieve the desired output? Thanks, Di, Yu 10.30 __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com

on Tue Oct 30 2007, Yu Di <diyu60607-AT-yahoo.com> wrote:
Hi, I am trying to use the preprocessor lib to generate something like
Cons<a, Cons<b, Cons<c, Cons<d, Nil> > > >
from
(a) (b) (c) (d)
The easy way to do things like this is to take advantage of the fact that the PP can generate invalid fragments of syntax, so something like: // not tested -- I may have the argument order wrong among other things #define cons_open(z, e, _) Cons<e, #define cons_close(z, e, _) > #define GENERATE(x) \ BOOST_PP_SEQ_FOR_EACH(cons_open, x, ~) Nil BOOST_PP_SEQ_FOR_EACH(cons_close,x,~) HTH, -- Dave Abrahams Boost Consulting http://www.boost-consulting.com
participants (2)
-
David Abrahams
-
Yu Di