Macros as arguments to preprocessor directives?

From § 16.2-4 ("Source file inclusion") of C++ 2003 draft.

Up vote 2 down vote favorite share g+ share fb share tw.

Being faced with the question wether its possible to choose #includes in the preprocessor I immediately thought not possible. .. Only to later find out that it is indeed possible and you only need to watch out for argument expansions (which e.g. Boost. Preprocessor can take care of).

While i'd avoid actually doing that for includes if possible, i'd like to know why this works. At the moment I fail to get a useful understanding in the C++ or C standard. Are parameterized macros allowed for any preprocessor-directive?

(except #define/#undef) Can someone reference where this is allowed and summarize it? Example for the curious utilizing Boost. Preprocessor for simplicity: #include #include #define INC_LOCAL(a,b) BOOST_PP_STRINGIZE(BOOST_PP_CAT(BOOST_PP_CAT(a,b),.

H)) #define INC_GLOBAL(a,b) BOOST_PP_CAT(BOOST_PP_CAT()) #include INC_LOCAL(loc,al) // #include "local. H" #include INC_GLOBAL(vect,or) // #include update: Referenced C standard, clarified question. C++ c preprocessor link|improve this question edited Nov 15 '09 at 5:54 asked Nov 15 '09 at 5:11Georg Fritzsche41.2k357105 88% accept rate.

From § 16.2-4 ("Source file inclusion") of C++ 2003 draft: A preprocessing directive of the form # include pp-tokens new-line (that does not match one of the two previous forms) is permitted. The preprocessing tokens after include in the directive are processed just as in normal text (each identifier currently defined as a macro name is replaced by its replacement list of preprocessing tokens). § 6.10.2-4 of C99 says the same.

The "two previous forms" mentioned above are # include and # include "q-char-sequence". The section seems too simple to summarize. For other directives, macro expansion isn't performed on any identifier preprocessing token (note this behavior is not defined by the grammar, but by C++ § 16 / C § 6.10): # if constant-expression new-line group # ifdef identifier new-line group # ifndef identifier new-line group # elif constant-expression new-line group # else new-line group # endif new-line # include pp-tokens new-line # define identifier replacement-list new-line # define identifier lparen identifier-list ) replacement-list new-line # undef identifier new-line # line pp-tokens new-line # error pp-tokens new-line # pragma pp-tokens new-line # new-line #line is explicitly macro-expanded by C++ § 16.4-5 / C § 6.10.4-5.

Expansion for #error (C++ § 16.5 / C § 6.10.5) and #pragma (C++ § 16.6 / C § 6.10.6) isn't mentioned. C++ § 16.3-7 / C 6.10.3-8 states: If a # preprocessing token, followed by an identifier, occurs lexically at the point at which a preprocessing directive could begin, the identifier is not subject to macro replacement. C++ § 16.3.1 / C § 6.10.3.1-1 tells us that when the arguments to a macro function are substituted into the replacement-list, they are first macro expanded.

Similarly, C++ § 16.3.4 / C § 6.10.3.4 has the preprocessor macro-expand the replacement-list after substitution. In summary, macro expansion is done for #if, #elif, #include, #line, the arguments to a macro function and the body of a macro function when substituted. I think that's everything.

Well, pp-tokens gets me to preprocessing-token, from where only preprocessing-op-or-punc looks good but only gives some operators. – Georg Fritzsche Nov 15 '09 at 5:50 So are processed just as in normal text implies macro expansion on something like identifier(identifier, ...)? – Georg Fritzsche Nov 15 '09 at 6:17 Sure.

The result might have undefined behavior. – outis Nov 15 '09 at 6:31 That clears my confusion, thanks for taking the time :) – Georg Fritzsche Nov 15 '09 at 6:33 I'm glad you could understand anything... though I do use BOOST_PP macros from time to time, it's all magic to me! I guess I'll have to read and reread this response over and over :/ – Matthieu M.

Nov 15 '09 at 14:34.

! ). I'm not sure how chapter and verse of the ISO C standard would help you -- the C++ standard, as I recall, does not change the preprocessor's operation anyway.

Correction: i'm now confused wether parameterized macros are simply allowed in any pp-directive and wanted to clearify that. – Georg Fritzsche Nov 15 '09 at 5:28.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions