c - Is a repeated macro invocation via token concatenation unspecified behavior? -
the c11 standard admits vagueness regard @ least 1 situation can arise in macro expansion, when function macro expands unenvoked name, , invoked next preprocessing token. example given in standard this.
#define f(a) a*g #define g(a) f(a) // may produce either 2*f(9) or 2*9*g f(2)(9)
that example not clarify happens when macro, m, expanded, , or part of result contributes via token concatenation second preprocessing token, m, invoked.
question: such invocation blocked?
here example of such invocation. issue tends come when using complicated set of macros, example contrived sake of simplicity.
// arity gives arity of args decimal integer (good 4 args) #define arity(...) arity_help(__va_args__,4,3,2,1,) #define arity_help(_1,_2,_3,_4,_5,...) _5 // define 'test' mimic 'arity' calling twice #define test(...) test_help_a( arity(__va_args__) ) #define test_help_a(k) test_help_b(k) #define test_help_b(k) test_help_##k #define test_help_1 arity(1) #define test_help_2 arity(1,2) #define test_help_3 arity(1,2,3) #define test_help_4 arity(1,2,3,4) // expand '1' or 'arity(1)'? test(x)
test(x)
expands test_help_a( arity(x) )
, invokes test_help_a
on rescanning, expands arg before substitution, , identical test_help_a(1)
, produces test_help_b(1)
, produces test_help_1
. clear.
so, question comes in here. test_help_1
produced using character, 1
, came expansion of arity
. can expansion of test_help_1
invoke arity again? versions of gcc , clang each think so.
can argue interpretations made gcc , clang required in standard?
is aware of implementation interprets situation differently?
i think gcc's , clang's interpretation correct. 2 expansions of arity
not in same call path. first descends expansion of test_help_a
's argument, second expansion of test_help_a
itself.
the idea of these rules guarantee there can't infinite recursion, guaranteed, here. there progress in evaluation of macro between 2 calls.
Comments
Post a Comment