Preprocessor magic needed

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • claus.tondering@gmail.com

    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

  • Eric Sosman

    #2
    Re: Preprocessor magic needed



    claus.tondering @gmail.com wrote On 03/17/06 09:26,:[color=blue]
    > 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)[/color]

    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.
    [color=blue]
    > This has to work in C, so I cannot use C++ templates.[/color]

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

    --
    Eric.Sosman@sun .com

    Comment

    • Paul Mensonides

      #3
      Re: Preprocessor magic needed

      claus.tondering @gmail.com wrote:[color=blue]
      > 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)[/color]

      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(i d) \
      BOOST_PP_EXPR_I IF( \
      BOOST_PP_COMPL( \
      BOOST_PP_IS_NUL LARY( \
      BOOST_PP_CAT(LI BRARY_MACRO_, id) \
      ) \
      ), \
      X BOOST_PP_CAT(m_ , X) \
      ) \
      /**/
      #define LIBRARY_MACRO_a lpha ()

      LIBRARY_MACRO(a lpha) // expands to nothing
      LIBRARY_MACRO(X ) // expands to X m_X

      Regards,
      Paul Mensonides


      Comment

      • Bob Hairgrove

        #4
        Re: Preprocessor magic needed

        On Sat, 18 Mar 2006 00:53:06 -0800, "Paul Mensonides"
        <pmenso57@comca st.net> wrote:
        [color=blue]
        >claus.tonderin g@gmail.com wrote:[color=green]
        >> 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)[/color]
        >
        >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(i d) \
        > BOOST_PP_EXPR_I IF( \
        > BOOST_PP_COMPL( \
        > BOOST_PP_IS_NUL LARY( \
        > BOOST_PP_CAT(LI BRARY_MACRO_, id) \
        > ) \
        > ), \
        > X BOOST_PP_CAT(m_ , X) \
        > ) \
        > /**/
        >#define LIBRARY_MACRO_a lpha ()
        >
        >LIBRARY_MACRO( alpha) // expands to nothing
        >LIBRARY_MACRO( X) // expands to X m_X
        >
        >Regards,
        >Paul Mensonides
        >[/color]

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

        --
        Bob Hairgrove
        NoSpamPlease@Ho me.com

        Comment

        • Paul Mensonides

          #5
          Re: Preprocessor magic needed

          Bob Hairgrove wrote:[color=blue]
          > On Sat, 18 Mar 2006 00:53:06 -0800, "Paul Mensonides"[/color]
          [color=blue][color=green]
          >> Regards,
          >> Paul Mensonides
          >>[/color]
          >
          > Hmmm ... he said it needs to work in C ... does Boost compile as C
          > code?[/color]

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

          Regards,
          Paul Mensonides


          Comment

          • claus.tondering@gmail.com

            #6
            Re: Preprocessor magic needed

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

            --
            Claus Tondering

            Comment

            • claus.tondering@gmail.com

              #7
              Re: Preprocessor magic needed

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

              --
              Claus Tondering

              Comment

              • claus.tondering@gmail.com

                #8
                Re: Preprocessor magic needed

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

                --
                Claus Tondering

                Comment

                • CBFalconer

                  #9
                  Re: Preprocessor magic needed

                  claus.tondering @gmail.com wrote:[color=blue]
                  >
                  > With the small change that the X in your macro should be replaced
                  > by id, it works beautifully! Thank you very much.[/color]

                  I suppose we should be thankful that such an incomprehensibl e 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.c om, 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/googlegroupsrep ly/>

                  Comment

                  Working...