
I must be doing something stupid. How can I process a sequence of sequences? I expected to be able to use nested invocations of SEQ_FOR_EACH, but I'm not getting the results I expect. Here's my code: #include <boost/preprocessor.hpp> #define INNER(R, DATA, ELEM)\ **ELEM**, #define OUTER(R, DATA, ELEM)\ << BOOST_PP_SEQ_FOR_EACH_R(R, INNER, ~, ELEM) >>, BOOST_PP_SEQ_FOR_EACH(OUTER, ~, ((1)(2)) ((3)(4)) ) I expected something like: << **1**, **2**, >>, << **3**, **4**, >>, But instead I'm getting: << BOOST_PP_SEQ_FOR_EACH_M(3, (INNER, ~, (1)(2) (nil))) BOOST_PP_SEQ_FOR_EACH_M(4, (INNER, ~, (2) (nil))) >>, << BOOST_PP_SEQ_FOR_EACH_M(4, (INNER, ~, (3)(4) (nil))) BOOST_PP_SEQ_FOR_EACH_M(5, (INNER, ~, (4) (nil))) >>, I've tried both msvc-8.0 and gcc-3.4. Can anybody see my mistake? -- Eric Niebler Boost Consulting www.boost-consulting.com

"Eric Niebler" <eric@boost-consulting.com> wrote
I must be doing something stupid. How can I process a sequence of sequences? I expected to be able to use nested invocations of SEQ_FOR_EACH, but I'm not getting the results I expect. Here's my code:
#include <boost/preprocessor.hpp>
#define INNER(R, DATA, ELEM)\ **ELEM**,
#define OUTER(R, DATA, ELEM)\ << BOOST_PP_SEQ_FOR_EACH_R(R, INNER, ~, ELEM) >>,
BOOST_PP_SEQ_FOR_EACH(OUTER, ~, ((1)(2)) ((3)(4)) )
I expected something like: << **1**, **2**, >>, << **3**, **4**, >>,
But instead I'm getting: << BOOST_PP_SEQ_FOR_EACH_M(3, (INNER, ~, (1)(2) (nil))) BOOST_PP_SEQ_FOR_EACH_M(4, (INNER, ~, (2) (nil))) >>, << BOOST_PP_SEQ_FOR_EACH_M(4, (INNER, ~, (3)(4) (nil))) BOOST_PP_SEQ_FOR_EACH_M(5, (INNER, ~, (4) (nil))) >>,
I've tried both msvc-8.0 and gcc-3.4. Can anybody see my mistake?
Unfortunately not every macro can be nested. IIUC, by default the preprocessor does not allow it. For some of macros special code is implemented inside Boost PP to allow nesting. If you use BOOST_PP_REPEAT, for instance, you should not have any problems. Regards, Arkadiy

Arkadiy Vertleyb wrote:
"Eric Niebler" <eric@boost-consulting.com> wrote
I must be doing something stupid. How can I process a sequence of sequences? I expected to be able to use nested invocations of SEQ_FOR_EACH, but I'm not getting the results I expect. Here's my code:
#include <boost/preprocessor.hpp>
#define INNER(R, DATA, ELEM)\ **ELEM**,
#define OUTER(R, DATA, ELEM)\ << BOOST_PP_SEQ_FOR_EACH_R(R, INNER, ~, ELEM) >>,
BOOST_PP_SEQ_FOR_EACH(OUTER, ~, ((1)(2)) ((3)(4)) )
I expected something like: << **1**, **2**, >>, << **3**, **4**, >>,
But instead I'm getting: << BOOST_PP_SEQ_FOR_EACH_M(3, (INNER, ~, (1)(2) (nil))) BOOST_PP_SEQ_FOR_EACH_M(4, (INNER, ~, (2) (nil))) >>, << BOOST_PP_SEQ_FOR_EACH_M(4, (INNER, ~, (3)(4) (nil))) BOOST_PP_SEQ_FOR_EACH_M(5, (INNER, ~, (4) (nil))) >>,
I've tried both msvc-8.0 and gcc-3.4. Can anybody see my mistake?
Unfortunately not every macro can be nested. IIUC, by default the preprocessor does not allow it. For some of macros special code is implemented inside Boost PP to allow nesting. If you use BOOST_PP_REPEAT, for instance, you should not have any problems.
That cannot be the answer. The sole reason that BOOST_PP_SEQ_FOR_EACH_R (note the trailing _R) exists is so that it can be reentered, IIUC. Is this a bug? -- Eric Niebler Boost Consulting www.boost-consulting.com

Eric Niebler wrote:
Arkadiy Vertleyb wrote: [...]
Unfortunately not every macro can be nested. IIUC, by default the preprocessor does not allow it. For some of macros special code is implemented inside Boost PP to allow nesting. If you use BOOST_PP_REPEAT, for instance, you should not have any problems.
That cannot be the answer. The sole reason that BOOST_PP_SEQ_FOR_EACH_R (note the trailing _R) exists is so that it can be reentered, IIUC. Is this a bug?
BOOST_PP_SEQ_FOR_EACH_R is just for efficiently reentering BOOST_PP_FOR, bypassing the automatic R detection. None of the higher level constructs can be reentered by them selfs. -- Daniel Wallin Boost Consulting www.boost-consulting.com

Arkadiy Vertleyb wrote:
"Eric Niebler" <eric@boost-consulting.com> wrote
I must be doing something stupid. How can I process a sequence of sequences? I expected to be able to use nested invocations of SEQ_FOR_EACH, but I'm not getting the results I expect. Here's my code: [...] I've tried both msvc-8.0 and gcc-3.4. Can anybody see my mistake?
Unfortunately not every macro can be nested. IIUC, by default the preprocessor does not allow it. For some of macros special code is implemented inside Boost PP to allow nesting. If you use BOOST_PP_REPEAT, for instance, you should not have any problems.
Also, BOOST_PP_SEQ_FOR_EACH_I is independent from BOOST_PP_SEQ_FOR_EACH, so if you only need two levels of nesting you can use both of them: #include <boost/preprocessor.hpp> #define INNER(R, DATA, I, ELEM)\ **ELEM**, #define OUTER(R, DATA, ELEM)\ << BOOST_PP_SEQ_FOR_EACH_I(INNER, ~, ELEM) >>, BOOST_PP_SEQ_FOR_EACH(OUTER, ~, ((1)(2)) ((3)(4)) ) gives the expected output. Otherwise you need to resort to either flattening the structure first, or doing your own iteration. HTH, -- Daniel Wallin Boost Consulting www.boost-consulting.com
participants (3)
-
Arkadiy Vertleyb
-
Daniel Wallin
-
Eric Niebler