Preprocessor magic needed

Discussion in 'C++' started by claus.tondering@gmail.com, Mar 17, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. Eric Sosman Guest

    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.

    --
     
    Eric Sosman, Mar 17, 2006
    #2
    1. Advertising

  3. 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
     
    Paul Mensonides, Mar 18, 2006
    #3
  4. 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
     
    Bob Hairgrove, Mar 18, 2006
    #4
  5. 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
     
    Paul Mensonides, Mar 18, 2006
    #5
  6. Guest

    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, 2006
    #6
  7. Guest

    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, 2006
    #7
  8. Guest

    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, 2006
    #8
  9. CBFalconer Guest

    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/>
     
    CBFalconer, Mar 20, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Preprocessor magic needed

    , Mar 17, 2006, in forum: C Programming
    Replies:
    8
    Views:
    353
    CBFalconer
    Mar 20, 2006
  2. mathieu
    Replies:
    8
    Views:
    479
    Alf P. Steinbach
    Aug 6, 2009
  3. mathieu
    Replies:
    1
    Views:
    545
    mathieu
    Aug 6, 2009
  4. Giles Bowkett
    Replies:
    9
    Views:
    429
    Giles Bowkett
    Dec 17, 2007
  5. Marcin Lukasik

    #define and preprocessor magic

    Marcin Lukasik, Nov 13, 2012, in forum: C Programming
    Replies:
    46
    Views:
    1,005
    Ben Bacarisse
    Nov 18, 2012
Loading...

Share This Page