How to generate random variable names in C++ using macros?

前端 未结 8 2022
别跟我提以往
别跟我提以往 2020-12-01 07:56

I\'m creating a macro in C++ that declares a variable and assigns some value to it. Depending on how the macro is used, the second occurrence of the macro can override the v

8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-01 08:53

    use __COUNTER__ (works on gcc4.8, clang 3.5 and Intel icc v13, MSVC 2015)

    #define CONCAT_(x,y) x##y
    #define CONCAT(x,y) CONCAT_(x,y)
    #define uniquename static bool CONCAT(sb_, __COUNTER__) = false
    

提交回复
热议问题