0

I am trying to create a "global variable" in the pre-processor that can be incremented.

For example, I have defined abc as 1. Can I redefine it into 2 next time (i got a redefinition error when i do this)? Do I need to first use undef? But I got compile error when using undef.

What is the proper way of doing something like this?

[100%] Building CXX object CMakeFiles/main.dir/main.cpp.o

error: use of undeclared identifier 'BOOST_PP_INC_abc'

    std::cout << temp << endl;

note: instantiated from:

    #define temp BOOST_PP_INC(abc)

note: instantiated from:

    #define BOOST_PP_INC(x) BOOST_PP_INC_I(x)
note: instantiated from:

    #define BOOST_PP_INC_I(x) BOOST_PP_INC_ ## x

<scratch space>:150:1: note: instantiated from: BOOST_PP_INC_abc

1 error generated.
make[2]: *** [CMakeFiles/main.dir/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/main.dir/all] Error 2
make: *** [all] Error 2

Here is the code

#include <iostream>
#include <boost/preprocessor/slot/counter.hpp>
#include <boost/preprocessor/arithmetic/add.hpp>
using namespace std;

int main() {
    std::cout << "Hello" << std::endl;

    #define abc 1
    #define temp BOOST_PP_INC(abc)
    #undef abc

    std::cout << temp << endl;
    return 0;
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Negative Zero
  • 1,224
  • 3
  • 10
  • 19
  • What exactly are you trying to do with this? There's probably a better way than macro abuse. – Mysticial Mar 01 '12 at 21:01
  • I am trying to keep track of the number abc. And then use it in the compile time. – Negative Zero Mar 01 '12 at 21:04
  • @NegativeZero, use cog: http://nedbatchelder.com/code/cog/ lot cleaner to work with that macro mess. Check this answer to see an example that might be relevant to you http://stackoverflow.com/questions/2506167/c-macros-with-memory/9455483#9455483 – lurscher Mar 01 '12 at 21:28
  • hi Lurscher, your link to the another stack overflow post is really helpful. I think that's something I wanted to do. It looks like i can change the value with the help of another header file. Let me investigate a little further. – Negative Zero Mar 01 '12 at 21:44
  • hmm... I am trying the answer to this question: http://stackoverflow.com/questions/2506167/c-macros-with-memory/9455483#9455483. But it's not compiling for me – Negative Zero Mar 01 '12 at 23:46

1 Answers1

2

You can't change the value of preprocessor macros inside the preprocessor I'm afraid. Perhaps you should rethink this in terms of the underlying goal you are trying to accomplish? You haven't stated what the underlying purpose to incrementing a preprocessor value would be.

Perry
  • 4,363
  • 1
  • 17
  • 20