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
On 10/2/07, Josh Brown
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.
There are links to GMane and others here: http://www.boost.org/more/mailing_lists.htm#users
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:
[snip]
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 :)
You'll want to look into BOOST_PP_TUPLE_EAT - http://www.boost.org/libs/preprocessor/doc/ref/tuple_eat.html --Michael Fawcett
Michael Fawcett wrote:
On 10/2/07, Josh Brown
wrote: 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?
Make the IF lazy.
Or are there any obvious other solutions to my problem that the boost experts can suggest :)
You'll want to look into BOOST_PP_TUPLE_EAT - http://www.boost.org/libs/preprocessor/doc/ref/tuple_eat.html
Exactly. #define GET_OPTIONAL_ELEMENT(r, data, elem) \ BOOST_PP_IF( \ BOOST_PP_EQUAL(BOOST_PP_SEQ_SIZE(elem), 3), \ BOOST_PP_SEQ_ELEM, BOOST_PP_TUPLE_EAT(2) \ )(2,elem) Regards, Tobias
Tobias Schwinger wrote:
Michael Fawcett wrote:
On 10/2/07, Josh Brown
wrote: 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?
Make the IF lazy.
Or are there any obvious other solutions to my problem that the boost experts can suggest :)
You'll want to look into BOOST_PP_TUPLE_EAT - http://www.boost.org/libs/preprocessor/doc/ref/tuple_eat.html
Exactly.
#define GET_OPTIONAL_ELEMENT(r, data, elem) \ BOOST_PP_IF( \ BOOST_PP_EQUAL(BOOST_PP_SEQ_SIZE(elem), 3), \ BOOST_PP_SEQ_ELEM, BOOST_PP_TUPLE_EAT(2) \ )(2,elem)
Ahh, thank you Tobias and Micheal for this suggestion. Last night I came up with the idea of pushing on an extra element to every sequence, that way when the SEQ_ELEM was expanded it would not error because there was always enough elements in the sequence, but since the bogus added element was never output it didn't hurt anything to have it there. However, your solution is an appropriate solution and not an ugly hack like mine :) Thanks again Josh this is much cleaner than the solution I came up with last night!
Regards, Tobias
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
on Tue Oct 02 2007, Josh Brown
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
The simplest way to deal with that problem might be to have the IF macro select one of two function-like macro *names*, and then invoke the one that's selected. -- Dave Abrahams Boost Consulting http://www.boost-consulting.com
participants (4)
-
David Abrahams
-
Josh Brown
-
Michael Fawcett
-
Tobias Schwinger