Prevent/control macro prescan

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • LCID Fire

    Prevent/control macro prescan

    Is there a way to prevent macro prescanning?
    I have the following problem:

    #define APE 4
    #define GET_ANIMAL_NAME (a, b) ANIMAL_##b##_## a
    #define CREATE_ANIMAL_E NTRY(a, b) GET_ANIMAL_NAME (a, b) = (a + b)

    enum animals
    {
    CREATE_ANIMAL_E NTRY(2, APE)
    };

    this expands to
    ANIMAL_4_2 = (2 + 4)
    but I'd like to prevent the prescanning (at least in GET_ANIMAL_NAME )
    to expand this to:
    ANIMAL_APE_2 = (2 + 4)

    I know it works when I move the content of GET_ANIMAL_NAME to
    CREATE_ANIMAL_E NTRY - but I need the name macro in other files, too.
  • Thad Smith

    #2
    Re: Prevent/control macro prescan

    LCID Fire wrote:
    Is there a way to prevent macro prescanning?
    I have the following problem:
    >
    #define APE 4
    #define GET_ANIMAL_NAME (a, b) ANIMAL_##b##_## a
    #define CREATE_ANIMAL_E NTRY(a, b) GET_ANIMAL_NAME (a, b) = (a + b)
    >
    enum animals
    {
    CREATE_ANIMAL_E NTRY(2, APE)
    };
    >
    this expands to
    ANIMAL_4_2 = (2 + 4)
    but I'd like to prevent the prescanning (at least in GET_ANIMAL_NAME )
    to expand this to:
    ANIMAL_APE_2 = (2 + 4)
    Normally that's a _feature_. In your case, use

    #define CREATE_ANIMAL_E NTRY(a, b) ANIMAL_##b##_## a = (a + b)

    --
    Thad

    Comment

    • Bart

      #3
      Re: Prevent/control macro prescan

      On May 18, 8:33 pm, LCID Fire <lcid-f...@gmx.netwro te:
      Is there a way to prevent macro prescanning?
      I have the following problem:
      >
      #define APE             4
      #define GET_ANIMAL_NAME (a, b) ANIMAL_##b##_## a
      #define CREATE_ANIMAL_E NTRY(a, b) GET_ANIMAL_NAME (a, b) = (a + b)
      >
      enum animals
      {
      CREATE_ANIMAL_E NTRY(2, APE)
      >
      };
      >
      this expands to
      ANIMAL_4_2 = (2 + 4)
      but I'd like to prevent the prescanning (at least in GET_ANIMAL_NAME )
      to expand this to:
      ANIMAL_APE_2 = (2 + 4)
      >
      I know it works when I move the content of GET_ANIMAL_NAME to
      CREATE_ANIMAL_E NTRY - but I need the name macro in other files, too.
      You may have to split APE into two identifiers. Here I've used APE and
      APE2:

      #define APE2 4
      #define GET_ANIMAL_NAME (a, b) ANIMAL_##b##_## a
      #define CREATE_ANIMAL_E NTRY(a, b) GET_ANIMAL_NAME (a, b) = (a + b##2)

      --
      Bartc

      Comment

      • LCID Fire

        #4
        Re: Prevent/control macro prescan

        On May 19, 3:57 am, Thad Smith <ThadSm...@acm. orgwrote:
        Normally that's a _feature_.  In your case, use
        >
        #define CREATE_ANIMAL_E NTRY(a, b) ANIMAL_##b##_## a = (a + b)
        As I stated it makes less sense to use this line because I want a
        single point in code to have this logic implemented (and use it in
        other files).
        To me the "feature" sound rather defunct when there is just one way
        and no way around it.

        Comment

        • Thad Smith

          #5
          Re: Prevent/control macro prescan

          LCID Fire wrote:
          Is there a way to prevent macro prescanning?
          I have the following problem:
          >
          #define APE 4
          #define GET_ANIMAL_NAME (a, b) ANIMAL_##b##_## a
          #define CREATE_ANIMAL_E NTRY(a, b) GET_ANIMAL_NAME (a, b) = (a + b)
          >
          enum animals
          {
          CREATE_ANIMAL_E NTRY(2, APE)
          };
          >
          this expands to
          ANIMAL_4_2 = (2 + 4)
          but I'd like to prevent the prescanning (at least in GET_ANIMAL_NAME )
          to expand this to:
          ANIMAL_APE_2 = (2 + 4)
          >
          I know it works when I move the content of GET_ANIMAL_NAME to
          CREATE_ANIMAL_E NTRY - but I need the name macro in other files, too.
          I failed to note you last sentence. One way to do this is with compound
          macros in which a single macro contains multiple fields. Here you want to
          be able to select either the name, APE, or an associated value 4. Here is
          one, admittedly ugly, way to do this:

          #define APE (APE,4) /* compound definition */

          /* ugly macros */
          #define ARG2_1(a,b) a
          #define ARG2_2(a,b) b
          #define ANIMAL_NAME(a) ARG2_1 a
          #define ANIMAL_NUM(a) ARG2_2 a
          #define GET_ANIMAL_NAME 3(a, b) ANIMAL_## b ##_## a
          #define GET_ANIMAL_NAME 2(a, b) GET_ANIMAL_NAME 3(a,b)
          #define GET_ANIMAL_NAME (a, b) GET_ANIMAL_NAME 2(a, ANIMAL_NAME(b))
          #define CREATE_ANIMAL_E NTRY(a, b) GET_ANIMAL_NAME (a, b) = \
          (a + ANIMAL_NUM(b))

          enum animals
          {
          CREATE_ANIMAL_E NTRY(2, APE),
          GET_ANIMAL_NAME (2,APE),
          ANIMAL_NAME(APE ),
          ANIMAL_NUM(APE)
          };

          This expands to (with my compiler)

          13: enum animals
          14: {
          15: ANIMAL_APE_2 = (2 + 4),
          16: ANIMAL_APE_2,
          17: APE,
          18: 4
          19: };

          I have used this technique to assign several values with a single name and
          use macros to extract the desired component.

          --
          Thad

          Comment

          Working...