incremented define?

前端 未结 4 1472
遥遥无期
遥遥无期 2021-01-13 15:48

Is there anyways to have a define increment every time you use it?

For example

int a = ADEFINE;
int b = ADEFINE;

a is 1 and b is 2.

4条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-13 16:10

    You can use __COUNTER__, though it's not standard. Both MSVC++ and GCC support it.


    If you can use boost, the pre-processor library has an implementation of counter. Here's the example from the documentation:

    #include 
    
    BOOST_PP_COUNTER // 0
    
    #include BOOST_PP_UPDATE_COUNTER()
    
    BOOST_PP_COUNTER // 1
    
    #include BOOST_PP_UPDATE_COUNTER()
    
    BOOST_PP_COUNTER // 2
    
    #include BOOST_PP_UPDATE_COUNTER()
    
    BOOST_PP_COUNTER // 3
    

    (Kudo's to gf)

提交回复
热议问题