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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Automatic macro increment

Automatic macro increment

Scheduled Pinned Locked Moved C / C++ / MFC
question
5 Posts 4 Posters 1 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.
  • J Offline
    J Offline
    Jesper Knudsen
    wrote on last edited by
    #1

    #define NEXT_INDEX ???? void something() { int A0 = NEXT_INDEX; //compiles as: int A0 = 0 int A1 = NEXT_INDEX; //compiles as: int A1 = 1 int A2 = NEXT_INDEX; //compiles as: int A2 = 2 ... ... } Hi, how can I define NEXT_INDEX macro so that it increments by one, each time the compiler parses it?

    C A 2 Replies Last reply
    0
    • J Jesper Knudsen

      #define NEXT_INDEX ???? void something() { int A0 = NEXT_INDEX; //compiles as: int A0 = 0 int A1 = NEXT_INDEX; //compiles as: int A1 = 1 int A2 = NEXT_INDEX; //compiles as: int A2 = 2 ... ... } Hi, how can I define NEXT_INDEX macro so that it increments by one, each time the compiler parses it?

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      You can use a static variable that you increment with the macro. Something that looks like: static int StaticIndex = 0; #define NEXT_INDEX StaticIndex++; This should work!

      J 1 Reply Last reply
      0
      • C Cedric Moonen

        You can use a static variable that you increment with the macro. Something that looks like: static int StaticIndex = 0; #define NEXT_INDEX StaticIndex++; This should work!

        J Offline
        J Offline
        Jesper Knudsen
        wrote on last edited by
        #3

        This would increment the index at runtime. What I need is the pre-processor to increment the index as it compiles so that the indices are found at compile time and fixed at runtime. Something like this (which will not compile..) #define AAA 0 #define NEXT_INDEX { AAA //insert as AAA #define AAA (AAA+1) //next time it is incremented by one } First time using NEXT_INDEX it compiles to AAA (which is 0 at first), next time I want it to compile to 1. This setup imo makes sence, but not to the compiler.. ;)

        D 1 Reply Last reply
        0
        • J Jesper Knudsen

          #define NEXT_INDEX ???? void something() { int A0 = NEXT_INDEX; //compiles as: int A0 = 0 int A1 = NEXT_INDEX; //compiles as: int A1 = 1 int A2 = NEXT_INDEX; //compiles as: int A2 = 2 ... ... } Hi, how can I define NEXT_INDEX macro so that it increments by one, each time the compiler parses it?

          A Offline
          A Offline
          Alvaro Mendez
          wrote on last edited by
          #4

          I'm pretty sure there's a clever template solution to this problem, but from looking at your example, I'm thinking it doesn't need to be too complicated. Going on the assumption that your variable will be named with the same value as it holds (such that VARn = n), then you can create a simple macro that defines it, like this:

          #define DEFINE_INDEX_VAR(prefix, number) const int prefix##number = number

          void something()
          {
          DEFINE_INDEX_VAR(A, 0);
          DEFINE_INDEX_VAR(A, 1);
          ...
          DEFINE_INDEX_VAR(Z, 1000);
          }

          Regards, Alvaro


          When birds fly in the right formation, they need only exert half the effort. Even in nature, teamwork results in collective laziness. -- despair.com

          1 Reply Last reply
          0
          • J Jesper Knudsen

            This would increment the index at runtime. What I need is the pre-processor to increment the index as it compiles so that the indices are found at compile time and fixed at runtime. Something like this (which will not compile..) #define AAA 0 #define NEXT_INDEX { AAA //insert as AAA #define AAA (AAA+1) //next time it is incremented by one } First time using NEXT_INDEX it compiles to AAA (which is 0 at first), next time I want it to compile to 1. This setup imo makes sence, but not to the compiler.. ;)

            D Offline
            D Offline
            Daniel Turini
            wrote on last edited by
            #5

            IMHO, there is no way of storing information at compile time. So, there is no way of storing a numbeer, incrementing and using it at compile time.


            It's not the fall that kills you: it's the sudden stop - Down by Law, Jim Jamursch (1986)

            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