Preprocessor magic needed

C

claus.tondering

I need to write a macro that inserts
someStruct m_someStruct;
into another struct declaration.

The problem is that if the programmer specifies one particluar struct
(called alpha), nothing should be inserted.

So, is it possible to write a macro that does this:

MACRO(alpha) expands to nothing
MACRO(X) expands to X m_##X (for all values of X except alpha)


My guess is that it is impossible. But then, in the Boost preprocessor
package, they achieve some things that I would also have thought
impossible. :)

This has to work in C, so I cannot use C++ templates.
 
E

Eric Sosman

I need to write a macro that inserts
someStruct m_someStruct;
into another struct declaration.

The problem is that if the programmer specifies one particluar struct
(called alpha), nothing should be inserted.

So, is it possible to write a macro that does this:

MACRO(alpha) expands to nothing
MACRO(X) expands to X m_##X (for all values of X except alpha)

The expansion of a macro cannot produce preprocessor
directives, hence it cannot produce preprocessor conditionals.

However, you might be able to do something like

#define alpha /* nil */
#define m_alpha /* nil */

.... so that when MACRO(alpha) expands, each component of its
expansion is also a macro that then "expands" to nothing.

Personally, I don't think the preprocessor is the right
tool for this.
This has to work in C, so I cannot use C++ templates.

Why cross-post to comp.lang.c++, then? Followups
set to comp.lang.c only.
 
P

Paul Mensonides

I need to write a macro that inserts
someStruct m_someStruct;
into another struct declaration.

The problem is that if the programmer specifies one particluar struct
(called alpha), nothing should be inserted.

So, is it possible to write a macro that does this:

MACRO(alpha) expands to nothing
MACRO(X) expands to X m_##X (for all values of X except alpha)

Yes, it is possible:

#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/control/expr_iif.hpp>
#include <boost/preprocessor/detail/is_nullary.hpp>
#include <boost/preprocessor/logical/compl.hpp>

#define LIBRARY_MACRO(id) \
BOOST_PP_EXPR_IIF( \
BOOST_PP_COMPL( \
BOOST_PP_IS_NULLARY( \
BOOST_PP_CAT(LIBRARY_MACRO_, id) \
) \
), \
X BOOST_PP_CAT(m_, X) \
) \
/**/
#define LIBRARY_MACRO_alpha ()

LIBRARY_MACRO(alpha) // expands to nothing
LIBRARY_MACRO(X) // expands to X m_X

Regards,
Paul Mensonides
 
B

Bob Hairgrove

Yes, it is possible:

#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/control/expr_iif.hpp>
#include <boost/preprocessor/detail/is_nullary.hpp>
#include <boost/preprocessor/logical/compl.hpp>

#define LIBRARY_MACRO(id) \
BOOST_PP_EXPR_IIF( \
BOOST_PP_COMPL( \
BOOST_PP_IS_NULLARY( \
BOOST_PP_CAT(LIBRARY_MACRO_, id) \
) \
), \
X BOOST_PP_CAT(m_, X) \
) \
/**/
#define LIBRARY_MACRO_alpha ()

LIBRARY_MACRO(alpha) // expands to nothing
LIBRARY_MACRO(X) // expands to X m_X

Regards,
Paul Mensonides

Hmmm ... he said it needs to work in C ... does Boost compile as C
code?
 
P

Paul Mensonides

Bob said:
Hmmm ... he said it needs to work in C ... does Boost compile as C
code?

The pp-lib is both C and C++ code. The rest of Boost is not.

Regards,
Paul Mensonides
 
C

claus.tondering

With the small change that the X in your macro should be replaced by
id, it works beautifully! Thank you very much.
 
C

claus.tondering

With the small change that the X in your macro should be replaced by
id, it works beautifully! Thank you very much.
 
C

claus.tondering

With the small change that the X in your macro should be replaced by
id, it works beautifully! Thank you very much.
 
C

CBFalconer

With the small change that the X in your macro should be replaced
by id, it works beautifully! Thank you very much.

I suppose we should be thankful that such an incomprehensible and
useless context free message, which was even stupidly cross-posted
to c.l.c++ and c.l.c, is short.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top