Creating C macro with ## and __LINE__ (token concatenation with positioning macro) -


I want to create a macro that creates a function with a name based on the line number. I thought I could do something like this (the actual function will contain statements within the braces):

  #define UNIQUE static zero unique _ ## __ line __ (zero) {}  

which I hoped would be an extension to do something like this:

  static zero unique_23 (zero) {}  

This does not work, with the token insertion, the positioning macros is really treated:

  static zero unique ___ line __ (zero) {}  

Is it The Nbv?

(Yes, there is a real reason that I want to do this, no matter how bad it is).

The problem is that when you have a macro replacement, the preprocess increases the macros only When neither the stringing operator # and the tokens-pasteing operator ## apply to it, you will have to use some extra layers, you can use a token-pasteing operator Recurring can be used with extended logic:

  #tokenpeste (x, w E) x ## y #define Define TOKENPASTE2 (x, y) tokenpeste (x, y) # Unique static zero tokenpost 2 (unique_, __LINE __) (zero) {}  

Then, the __ line __ becomes expanded into the line number during the expansion of UNIQUE (because it is # or # # is not included), and then the token is pasting during the expansion of TOKENPASTE

It should also be noted that there are __ COUNTER __ is a macro, which is Once a new integer is evaluated, if you need multiple installation of the UNIQUE macro on the same line Note: __ COUNTER __ MS Visual Studio, GCC ( V. 4.3) and supported by Clang, but not standard C.


Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -