Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reusable preprocessor __COUNTER__

I am doing some template meta programming, mostly just writing my own compile time list, but I also have some preprocessor magic which I want to use to make things easier if possible.

What I am trying to do is create a compile time list of functors. That part is done, but the macros to ease creation (and add to the list) are not.

An example in brief:

template<typename Functor, typename Tail>
struct node {
    typedef Functor head;
    typedef Tail tail;
};


template <typename Functor, typename Tail>
struct push_back {
    typedef node<Functor, Tail> list;
};

struct unit0 {};

#define AUTO_FUNCTION(name) struct test_functor_##name {            \
    static void run_test();                                         \
};                                                                  \
typedef push_back<                                                  \
            test_functor_##name,                                    \
            CONCAT(unit, PP_DEC(__COUNTER__))                       \
        >::list CONCAT(unit, __COUNTER__);                          \
void test_functor_##name::run_test()


AUTO_FUNCTION(hello) {
    ...
}

Now, this works because I have created a large set of preprocessor macros for PP_DEC, ie:

#define PP_DEC(x) PP_DEC_I(x)
#define PP_DEC_I(x) PP_DEC_ ## x
#define PP_DEC_1 0
#define PP_DEC_2 1
...
#define PP_DEC_N N

That's the part I really want to avoid and the reason I am asking this question. Does anyone have a suggestion on how I can use COUNTER without increasing its value, or some other way I can accomplish a counting pattern similar to:

 0 1
 1 2
 2 3
 ...

Suggestions that change the semantics of push_back, etc. are of course also welcome :).

PS. This is not meant for production, only for fun. So GCC specific extensions are welcomed.

PPS. I am trying to avoid external dependencies, such as boost, as I want to understand everything I am doing (the whole point of this project).

like image 285
Mic Avatar asked Jan 18 '11 01:01

Mic


1 Answers

You can "fix" __COUNTER__ value by using an extra macro:

#define MY_MACRO MY_MACRO_COUNTED(__COUNTER__)

#define MY_MACRO_COUNTED(counter) counter + counter
like image 77
Grigory Entin Avatar answered Sep 28 '22 17:09

Grigory Entin