Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Preprocessor magic needed

Reply
Thread Tools

Preprocessor magic needed

 
 
claus.tondering@gmail.com
Guest
Posts: n/a
 
      03-17-2006
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.

--
Claus Tondering

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      03-17-2006


wrote On 03/17/06 09:26,:
> 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.

--


 
Reply With Quote
 
 
 
 
Paul Mensonides
Guest
Posts: n/a
 
      03-18-2006
wrote:
> 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


 
Reply With Quote
 
Bob Hairgrove
Guest
Posts: n/a
 
      03-18-2006
On Sat, 18 Mar 2006 00:53:06 -0800, "Paul Mensonides"
<> wrote:

> wrote:
>> 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
>


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

--
Bob Hairgrove

 
Reply With Quote
 
Paul Mensonides
Guest
Posts: n/a
 
      03-18-2006
Bob Hairgrove wrote:
> On Sat, 18 Mar 2006 00:53:06 -0800, "Paul Mensonides"


>> Regards,
>> Paul Mensonides
>>

>
> 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


 
Reply With Quote
 
claus.tondering@gmail.com
Guest
Posts: n/a
 
      03-20-2006
With the small change that the X in your macro should be replaced by
id, it works beautifully! Thank you very much.

--
Claus Tondering

 
Reply With Quote
 
claus.tondering@gmail.com
Guest
Posts: n/a
 
      03-20-2006
With the small change that the X in your macro should be replaced by
id, it works beautifully! Thank you very much.

--
Claus Tondering

 
Reply With Quote
 
claus.tondering@gmail.com
Guest
Posts: n/a
 
      03-20-2006
With the small change that the X in your macro should be replaced by
id, it works beautifully! Thank you very much.

--
Claus Tondering

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      03-20-2006
wrote:
>
> 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/>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Preprocessor magic to expand template instantiation ? mathieu C++ 8 08-06-2009 10:13 PM
Preprocessor magic needed claus.tondering@gmail.com C Programming 8 03-20-2006 10:52 AM
Compiler error occurred when try to use a flexible template expression in preprocessor definesCompiler error occurred when try to use a flexible template expression in preprocessor defines snnn C++ 6 03-14-2005 04:09 PM
Cinema.Craft.Encoder.SP.v2.70.01.05, Magic.Bullet.Editor.v.1.01.for.Avid.Xpress.Pro, Magic.Bullet.Editor.v.1.01.for.Premiere.Pro, Magic.Bullet.Editor.v.1.01.for.Sony.Vegas, Avid.Xpress.Pro.HD.v5.0 1CD, Sony.Vegas.v5.0d, ola DVD Video 0 01-14-2005 10:53 AM
preprocessor, token concatenation, no valid preprocessor token Cronus C++ 1 07-14-2004 11:10 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57