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

前端 未结 8 2026
别跟我提以往
别跟我提以往 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:37

    Add M4 to your build flow? This macro language has some stateful capabilities, and can successfully be intermingled with CPP macros. This is probably not a standard way to generate unique names in a C environment, though I've been able to sucessfully use it in such a manner.

    You probably do not not want random, BTW, based on the way you posed your question. You want unique.

    You could use __FILE__ and __LINE__ in the macro expansion to get you the uniqueness you seem to be going for... those metavariables get defined within the source file context, so be careful to make sure you get what you are looking for (e.g., perils of more than one macro on the same line).

提交回复
热议问题