Reusable preprocessor __COUNTER__

谁都会走 提交于 2019-12-06 04:19:19

问题


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).


回答1:


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

#define MY_MACRO MY_MACRO_COUNTED(__COUNTER__)

#define MY_MACRO_COUNTED(counter) counter + counter


来源:https://stackoverflow.com/questions/4719568/reusable-preprocessor-counter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!