gcc - Make c++ macro2 containt quoted body of macro1 -


i'm trying make kind of simple system calculates number of builds, including info in .rc file (for windows) , met problem. here is:

#define quote(s) #s #define       0,0,0,1 #define a_str   quote(a) 

expanding of a_str: "a" not "0,0,0,1" expected.

well, need a_str string representation of a (that's windres expects see in .rc file), can't find way this.

i've tried smth #define a_str #a expands #0,0,0,1.

i tried using qmake this: defines *= a_str="<here-is-how-i-get-version>" gcc gets without quotes , i've got same problem.

when c preprocessor macro expanded, parameters expanded literal arguments, s expanded a when quote(s) taking argument a expanded. normally, after expansion complete, expanded text scanned again expand macros embedded therein, cause a expand 0,0,0,1. however, when stringification operator # used stringify following text, stringification happens first, following text never gets chance expanded, stringified "a" final expansion of a_str.

this problem solved introducing second level of indirection, gives initial macro argument second chance expand:

#define quote2(a) #a #define quote(a) quote2(a) 

however, not work case, because in first-level expansion a expand 0,0,0,1, taken 4 arguments quote2(), , rejected invalid macro call.

you can solve variadic macro arguments , __va_args__:

#define quote2(...) #__va_args__ #define quote(...) quote2(__va_args__) 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -