32
If an argument is stringified or concatenated, the prescan does not occur. If you want to expand a
macro, then stringify or concatenate its expansion, you can do that by causing one macro to call
another macro that does the stringification or concatenation. For instance, if you have
#define AFTERX(x) X_ ## x
#define XAFTERX(x) AFTERX(x)
#define TABLESIZE 1024
#define BUFSIZE TABLESIZE
then
AFTERX(BUFSIZE)
(Not to
X_TABLESIZE
Macros used in arguments, whose expansions contain unshielded commas.
•
This can cause a macro expanded on the second scan to be called with the wrong number of argu-
ments. Here is an example:
#define foo
a,b
#define bar(x) lose(x)
#define lose(x) (1 + (x))
We would like
bar(foo)
Instead,
bar(foo)
argument. In this case, the problem is easily solved by the same parentheses that ought to be used
to prevent misnesting of arithmetic operations:
#define foo (a,b)
or
#define bar(x) lose((x))
The extra pair of parentheses prevents the comma in
argument separator.
3.9.7. Newlines in Arguments
The invocation of a function-like macro can extend over many logical lines. However, in the present
implementation, the entire expansion comes out on one line. Thus line numbers emitted by the com-
piler or debugger refer to the line the invocation started on, which might be different to the line
containing the argument causing the problem.
Here is an example illustrating this:
#define ignore_second_arg(a,b,c) a; c
ignore_second_arg (foo (),
The syntax error triggered by the tokens
-the line of ignore_second_arg-- even though the problematic code comes from line five.
We consider this a bug, and intend to fix it in the near future.
expands to
X_BUFSIZE
. Prescan always does a complete expansion.)
to turn into
(1 + (foo))
expands into
lose(a,b)
ignored (),
syntax error);
syntax error
, and
XAFTERX(BUFSIZE)
, which would then turn into
, and you get an error because
's definition from being interpreted as an
foo
results in an error message citing line three-
Chapter 3. Macros
expands to
X_1024
(1 + (a,b))
requires a single
lose
.
.
Need help?
Do you have a question about the ENTERPRISE LINUX 3 - USING CPP and is the answer not in the manual?
Questions and answers