473,396 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Preprocessor magic needed

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

Mar 17 '06 #1
8 2872


cl*************@gmail.com 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.

--
Er*********@sun.com

Mar 17 '06 #2
cl*************@gmail.com 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
Mar 18 '06 #3
On Sat, 18 Mar 2006 00:53:06 -0800, "Paul Mensonides"
<pm******@comcast.net> wrote:
cl*************@gmail.com 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
No**********@Home.com
Mar 18 '06 #4
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
Mar 18 '06 #5
With the small change that the X in your macro should be replaced by
id, it works beautifully! Thank you very much.

--
Claus Tondering

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

--
Claus Tondering

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

--
Claus Tondering

Mar 20 '06 #8
cl*************@gmail.com 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/>

Mar 20 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: seemanta dutta | last post by:
Greetings C gurus, I have used preprocessor directives since a very long time. But whenever I see some professional piece of C code, the linux kernel for example, I get literally confused by the...
3
by: Christopher Benson-Manica | last post by:
If I have a symbol defined at preprocessing (assume it's all digits), how can I make that symbol into a string constant for compiling? -- Christopher Benson-Manica | I *should* know what I'm...
9
by: ccwork | last post by:
Hi all, We can define some magic number with #define: #define OPERATION_1 0x00000001 #define OPERATION_2 0x00000002 #define OPERATION_3 0x00000003 .... We can also do that with enum: enum...
8
by: claus.tondering | last post by:
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...
10
by: Jon | last post by:
All, Yes, it's more of the famous 'what do I do about magic_quotes' questions. Anyways, here we go: I've been a PHP developer for about a year now, and have grown to detest magic_quotes for...
32
by: spibou | last post by:
Is the output of the C preprocessor deterministic ? What I mean by that is , given 2 compilers which conform to the same standard, will their preprocessors produce identical output given as input...
13
by: josh | last post by:
Hi, I've read that in the future the C++ will not more support the preprocessor and now some preprocessor functionality have already been implemented in the language itself. What are that? ...
9
by: Larry Hale | last post by:
I've heard tell of a Python binding for libmagic (file(1) *nixy command; see http://darwinsys.com/file/). Generally, has anybody built this and worked with it under Windows? The only thing I've...
7
by: Rohit | last post by:
Hi, I am working on a switch module which after reading voltage through a port pin and caterogizing it into three ranges(open,low or high), passes this range to a function switch_status() with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.