Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Passing a "value", that was either #defined or #undef, into a macro and check if it was defined?

Passing a "value", that was either #defined or #undef, into a macro and check if it was defined?

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    arnold_w
    wrote on last edited by
    #1

    I want to create a macro that is fully executed at compile time (not runtime). If two values are both defined, then I want to check if they are equal. If at least one of them is not defined, then nothing should happen. I have come halfway, the following works great:

    #define VALIDATE_ADDR(ADDR1,ADDR2) _Static_assert(ADDR1 == ADDR2, #ADDR1 " is not equal to " #ADDR2);

    However, when I try to nest that macro into another macro, then I run into trouble:

    #define VALIDATE_ADDR_IF_THEY_BOTH_EXIST(ADDR1,ADDR2) \
    #if defined(ADDR1) && defined(ADDR2) \
    VALIDATE_ADDR(ADDR1, ADDR2) \
    #endif

    Can someone please help me so I can do checks like this:

    #define MY_ADDR_1 (1)
    #define MY_ADDR_2 (2)
    #define MY_ADDR_3 (3)
    #ifdef MY_ADDR_4
    #undef MY_ADDR_4
    #endif

    VALIDATE_ADDR_IF_THEY_BOTH_EXIST(MY_ADDR_1, MY_ADDR_2); // Compile time error since MY_ADDR_1 != MY_ADDR_2
    VALIDATE_ADDR_IF_THEY_BOTH_EXIST(MY_ADDR_3, MY_ADDR_4); // Compiles just fine since MY_ADDR_4 is not defined

    K L A 3 Replies Last reply
    0
    • A arnold_w

      I want to create a macro that is fully executed at compile time (not runtime). If two values are both defined, then I want to check if they are equal. If at least one of them is not defined, then nothing should happen. I have come halfway, the following works great:

      #define VALIDATE_ADDR(ADDR1,ADDR2) _Static_assert(ADDR1 == ADDR2, #ADDR1 " is not equal to " #ADDR2);

      However, when I try to nest that macro into another macro, then I run into trouble:

      #define VALIDATE_ADDR_IF_THEY_BOTH_EXIST(ADDR1,ADDR2) \
      #if defined(ADDR1) && defined(ADDR2) \
      VALIDATE_ADDR(ADDR1, ADDR2) \
      #endif

      Can someone please help me so I can do checks like this:

      #define MY_ADDR_1 (1)
      #define MY_ADDR_2 (2)
      #define MY_ADDR_3 (3)
      #ifdef MY_ADDR_4
      #undef MY_ADDR_4
      #endif

      VALIDATE_ADDR_IF_THEY_BOTH_EXIST(MY_ADDR_1, MY_ADDR_2); // Compile time error since MY_ADDR_1 != MY_ADDR_2
      VALIDATE_ADDR_IF_THEY_BOTH_EXIST(MY_ADDR_3, MY_ADDR_4); // Compiles just fine since MY_ADDR_4 is not defined

      K Offline
      K Offline
      k5054
      wrote on last edited by
      #2

      AFAIK, that's not possible, since the # token is reserved for stringifying macro parameters within the macro expansion. Also, I see that your definition for VALIDATE_ADDR ends in a semicolon. In general, you do not want to end a macro with a semicolon, since this can lead to, in the best case, a compiler error, and in the worst case, a subtle and hard to find bug.

      1 Reply Last reply
      0
      • A arnold_w

        I want to create a macro that is fully executed at compile time (not runtime). If two values are both defined, then I want to check if they are equal. If at least one of them is not defined, then nothing should happen. I have come halfway, the following works great:

        #define VALIDATE_ADDR(ADDR1,ADDR2) _Static_assert(ADDR1 == ADDR2, #ADDR1 " is not equal to " #ADDR2);

        However, when I try to nest that macro into another macro, then I run into trouble:

        #define VALIDATE_ADDR_IF_THEY_BOTH_EXIST(ADDR1,ADDR2) \
        #if defined(ADDR1) && defined(ADDR2) \
        VALIDATE_ADDR(ADDR1, ADDR2) \
        #endif

        Can someone please help me so I can do checks like this:

        #define MY_ADDR_1 (1)
        #define MY_ADDR_2 (2)
        #define MY_ADDR_3 (3)
        #ifdef MY_ADDR_4
        #undef MY_ADDR_4
        #endif

        VALIDATE_ADDR_IF_THEY_BOTH_EXIST(MY_ADDR_1, MY_ADDR_2); // Compile time error since MY_ADDR_1 != MY_ADDR_2
        VALIDATE_ADDR_IF_THEY_BOTH_EXIST(MY_ADDR_3, MY_ADDR_4); // Compiles just fine since MY_ADDR_4 is not defined

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Macros are purely compile time. If you want runtime checks then use functions. Macros such as the above tend to be more trouble than they are worth.

        A 1 Reply Last reply
        0
        • L Lost User

          Macros are purely compile time. If you want runtime checks then use functions. Macros such as the above tend to be more trouble than they are worth.

          A Offline
          A Offline
          arnold_w
          wrote on last edited by
          #4

          What do you mean? I've clearly stated I want compile time execution, not runtime execution of this macro. Are you thinking of the _Static_assert function? It is executed at compile time, I think it's a feature built into the gcc compiler.

          L 1 Reply Last reply
          0
          • A arnold_w

            I want to create a macro that is fully executed at compile time (not runtime). If two values are both defined, then I want to check if they are equal. If at least one of them is not defined, then nothing should happen. I have come halfway, the following works great:

            #define VALIDATE_ADDR(ADDR1,ADDR2) _Static_assert(ADDR1 == ADDR2, #ADDR1 " is not equal to " #ADDR2);

            However, when I try to nest that macro into another macro, then I run into trouble:

            #define VALIDATE_ADDR_IF_THEY_BOTH_EXIST(ADDR1,ADDR2) \
            #if defined(ADDR1) && defined(ADDR2) \
            VALIDATE_ADDR(ADDR1, ADDR2) \
            #endif

            Can someone please help me so I can do checks like this:

            #define MY_ADDR_1 (1)
            #define MY_ADDR_2 (2)
            #define MY_ADDR_3 (3)
            #ifdef MY_ADDR_4
            #undef MY_ADDR_4
            #endif

            VALIDATE_ADDR_IF_THEY_BOTH_EXIST(MY_ADDR_1, MY_ADDR_2); // Compile time error since MY_ADDR_1 != MY_ADDR_2
            VALIDATE_ADDR_IF_THEY_BOTH_EXIST(MY_ADDR_3, MY_ADDR_4); // Compiles just fine since MY_ADDR_4 is not defined

            A Offline
            A Offline
            arnold_w
            wrote on last edited by
            #5

            Btw, I don't need the VALIDATE_ADDR macro for anything, it's perfectly fine to replace it with the _Static_assert directly, if that makes anything easier.

            1 Reply Last reply
            0
            • A arnold_w

              What do you mean? I've clearly stated I want compile time execution, not runtime execution of this macro. Are you thinking of the _Static_assert function? It is executed at compile time, I think it's a feature built into the gcc compiler.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Sorry I misread the question. I have tried a number of variants but cannot get it to work. it would appear that #define statements are not accepted inside a macro definition.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups