Why are argument substitutions not replaced during rescanning?

前端 未结 1 1285
鱼传尺愫
鱼传尺愫 2021-02-20 01:36

Consider the following macro definitions and invocation:

#define x x[0]
#define y(arg) arg

y(x)

This invocation expands to x[0] (te

1条回答
  •  孤街浪徒
    2021-02-20 02:25

    I believe you've quoted the crucial paragraph, you just stopped too soon. 16.3.4/2 (emphasis mine):

    If the name of the macro being replaced is found during this scan of the replacement list (not including the rest of the source file’s preprocessing tokens), it is not replaced. Further, if any nested replacements encounter the name of the macro being replaced, it is not replaced. These nonreplaced macro name preprocessing tokens are no longer available for further replacement even if they are later (re)examined in contexts in which that macro name preprocessing token would otherwise have been replaced.

    So, when x is replaced with x[0] during the parameter substitution of y, it is fully macro replaced, meaning that it is rescanned at that point, and x is caught by the recursion rule. This means that the x in x[0] is no longer eligible for further replacement, including during the rescanning of the partly-expanded result of y(x).

    0 讨论(0)
提交回复
热议问题