Boost logo

Boost Users :

From: Josh Brown (jbrown_at_[hidden])
Date: 2007-10-02 16:13:10


Hello,

First off, I apologize if this is a redundant post, I'm relatively new
to Boost and I couldn't find a link to a list archive and a google
search didn't merit anything either.

I'm using the preprocessor lib to generate a class definition from a
sequence of sequences. I want to be able to make the last element of
each sequence optional and generate additional code based on if the last
parameter exists. I'm currently testing for the length of the sequence
in a BOOST_PP_IF statement to see if it has the optional last
parameter. The problem is that both the true and false result
expressions are being expanded and then the correct one is selected
based on the evaluation; which in my case causes an error. I boiled
down my problem to a fairly simple example:

//= test.cpp =================================================
#include <boost/preprocessor/seq/for_each.hpp>
#include <boost/preprocessor/control/if.hpp>
#include <boost/preprocessor/comparison/equal.hpp>
#include <boost/preprocessor/seq/size.hpp>
#include <boost/preprocessor/seq/elem.hpp>
#include <boost/preprocessor/facilities/empty.hpp>

#define MY_SEQ ((var1) (VAR1) (option1)) ((var2) (VAR2))

#define GET_OPTIONAL_ELEMENT(r, data, elem) \
    BOOST_PP_IF( \
        BOOST_PP_EQUAL(BOOST_PP_SEQ_SIZE(elem), 3), \
        BOOST_PP_SEQ_ELEM(2, elem), \
        BOOST_PP_EMPTY() \
    )

#define DEMO_PROBLEM(seq) \
    BOOST_PP_SEQ_FOR_EACH(GET_OPTIONAL_ELEMENT, _, seq)

DEMO_PROBLEM(MY_SEQ)
//= test.cpp =================================================

running the above code through gcc -E generates this error:
test.cpp:20:1: error: macro "BOOST_PP_SEQ_ELEM_III" requires 2
arguments, but only 1 given

The root of my problem seems to stem from the fact that BOOST_PP_IF
expands both the true and false expressions before it does the if test
and just outputs the already expanded expression. Is this correct; and
if so, is there a way around it? Or are there any obvious other
solutions to my problem that the boost experts can suggest :)

Thanks
Josh Brown


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net