C++ – Force Preprocessor to use a previous definition in a redefinition

boost-preprocessorcc-preprocessorg++

Update 3:

Never mind. I kinda got what I was looking for. The following gives unique identifiers inside a class.

static const int _counter_start = __COUNTER__;
static const int val1 = __COUNTER__ - _counter_start;
static const int val2 = __COUNTER__ - _counter_start;

Update 2:

Boost Preprocessor

I will be implementing something akin to a message map with this functionality.

class a
{
...
    MAP_BEGIN()
    MAP_DECL...
    MAP_END()
...
};

The thing is, for each MAP_DECL, I need to expand the macro in 2 places.

class a
{    
    virtual void func()
    {        
        ...
        //does something with the decl declaration        
    }
    ...
    //also expand some stuff here    
}

Boost preprocessor should (theoretically) allow me to accumulate MAP_DECL into a sequence and expand it into func() at the end (while simultaneously expanding the class fields as we go).


Update 1:

I'm using the Boost Preprocessor library at the moment. I'm currently stuck creating a new macro variable/definition like the following every time I need to add something to the sequence.

I'm trying to expand Boost Preprocessor sequences and I'm stuck doing this at the moment

#define SEQ (w)(x)(y)(z) 
#define SEQ2 BOOST_PP_SEQ_PUSH_BACK(SEQ, a)

Original:

Suppose I have the following code

#define CUR 2
#define CUR CUR + 2

How do I force the 2nd line to use the value of CUR from the first line?

Best Answer

Succinctly, you can't.

At the time when CUR is expanded (after the second #define), the preprocessor will replace an instance of CUR with CUR + 2, and 'blue paint' the name CUR (not expanding it any further). Thereafter, the C compiler sees CUR + 2, which most likely yields a compilation error.