Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can (or could) one suppress macro replacement in C by enclosing the name in brackets

Tags:

c

macros

I seem to recall that one could suppress replacement (expansion) of a macro in C by placing the macro name in brackets, e.g. (free)(p) would call the function free whether or not a macro free were defined. I see no mention of this in the C99 standard (it is there, see answer), and I observe that MSVS 2013 does not implement it either. Added in light of answer: It does, just as the standard requires, i.e. only for function-like macros, whose expansion is triggered by a following ‘( and thus inhibited by the intervening ‘)’.

Am I dreaming, or was there such a possibility, and if so, what was the rationale for withdrawing it? Or was present only in certain dialects?

like image 643
PJTraill Avatar asked Jan 15 '16 20:01

PJTraill


1 Answers

A function-like macro FOO

#define FOO(x) ...

is only expanded when the token FOO appears followed by a ( token. Thus, to prevent the expansion of FOO, (FOO) can be used. Just as you said. This however only applies to function-like macros.

This is specified in ISO 9899:2011 §6.10.3 ¶10, which reads:

10 A preprocessing directive of the form

# define identifier lparen identifier-listopt ) replacement-list new-line
# define identifier lparen ... ) replacement-list new-line
# define identifier lparen identifier-list , ... ) replacement-list new-line

defines a function-like macro with parameters, whose use is similar syntactically to a function call. The parameters are specified by the optional list of identifiers, whose scope extends from their declaration in the identifier list until the new-line character that terminates the #define preprocessing directive. Each subsequent instance of the function-like macro name followed by a ( as the next preprocessing token introduces the sequence of preprocessing tokens that is replaced by the replacement list in the definition (an invocation of the macro). The replaced sequence of preprocessing tokens is terminated by the matching ) preprocessing token, skipping intervening matched pairs of left and right parenthesis preprocessing tokens. Within the sequence of preprocessing tokens making up an invocation of a function-like macro, new-line is considered a normal white-space character.

like image 163
fuz Avatar answered Oct 22 '22 06:10

fuz